2

I'm working on a project, and we use a lot of coding languages for different parts.

I've tried to simplify the problem as much as I can, and came up with the following JSfiddle.

The problem is, I have a JSON string which I want to pass on to a JavaScript function. If I call it from JavaScript, it's OK. But when I call it with a onmouseover, HTML throws the error

Uncaught SyntaxError: Invalid or unexpected token

JavaScript Code:

 function test123(obj, e, lookupx){
      console.log(lookupx);
    }

    test123(this, event, '{"mt:assetsys:assetuapr":{"assetmat":"material","assettag":"tag"}}');

HTML:

    <div>
      First check the console, direct calling with js string is ok.<br>
      Then:<br>
      <a href="#" onmouseover="test123(this, event, '{"mt:assetsys:assetuapr": 
                 {"assetmat":"material","assettag":"tag"}}');">hover this
      </a>
      <br>
      And an error occurs
    </div>

Could someone explain what the difference is between the two cases?

  1. The onmouseover vs

  2. The direct calling to the function.

3
  • 2
    Escape the inner ", e.g. \" Commented Jun 15, 2018 at 14:02
  • because in the second case the JSON string is wrapped inside double-quotes already (for the onmouseover attribute), so you need to escape the double-quotes in the JSON, otherwise the onmouseover attribute will be malformed - the first time it comes to a double-quote related to the JSON it thinks it's the end of the onmouseover attribute, but it's not. Commented Jun 15, 2018 at 14:03
  • Of course, thank you both for the clarification. Commented Jun 16, 2018 at 8:52

3 Answers 3

3

I had the same scenario, I ended up using Template literal

Note, you have to use single quotes ' for HTML attribute value.

So your code can be re-written as follows,

<div>
  First check the console, direct calling with js string is ok.<br>
  Then:<br>
  <a href="#" onmouseover='test123(this, event, `{"mt:assetsys:assetuapr":{"assetmat":"material","assettag":"tag"}}`);'>
   hover this
  </a><br>
  And an error occurs
</div>

Link to jsFiddle

Sign up to request clarification or add additional context in comments.

1 Comment

This works perfectly, thank you! In my honest opinion, the readability of this answer is much better than when you escape them (like in the other answers). Ty for the link to template literal.
2

You have to escape all the quotes inside the onmousover=""

Escape them with a backslash like this \"

1 Comment

Silly me, I can do that of course. The answer Gaurav Gandhi gave is slightly more readable. But thank you none the less!
1

You are not escaping the character, also you need to make some adjustment like I did for your Updated JSFiddle

Check it , it works fine

HTML

<div>
  First check the console, direct calling with js string is ok.<br>
  Then:<br>
  <a href="#" onmouseover='test123(this, event, "{\"mt:assetsys:assetuapr\":{\"assetmat\":\"material\",\"assettag\":\"tag\"}}")'>
   hover this
  </a><br>
  And an error occurs
</div>

JS

function test123(obj, e, lookupx){
 console.log(lookupx);
}

test123(this, event, '{"mt:assetsys:assetuapr":{"assetmat":"material","assettag":"tag"}}');

Hope it helps!!

4 Comments

Thank you for the answer, it cleared a few things up. The code isn't working 100%, but I get the gist :)
but it is working , i checked in myself in the console. :)
Well, I'll be... I'm sorry, your JSFiddle works indeed. Yet when I copy and paste the code given in your 2 blocks (HTML and JS) and paste it in JSFiddle myself, it returns: 'Uncaught ReferenceError: test123 is not defined'. But I can't, for the life of me, spot the difference.. Strange, but nevertheless, thank you :)
may be you need to select , No wrap - bottom of <head>, in JS load type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.