JavaScript Passing JSON string to JavaScript passing json string as query parameter,passing json string to

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 target='_blank' 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.

Answer:1

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 target='_blank' href="#" onmouseover='test123(this, event, `{"mt:assetsys:assetuapr":{"assetmat":"material","assettag":"tag"}}`);'>
   hover this
  </a><br>
  And an error occurs
</div>

Link to jsFiddle

Answer:2

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

Escape them with a backslash like this \"

Answer:3

I'm working on my first GatsbyJS project (and hosted on Netlify) and over the past few weeks have noticed that everything is very speedy during development, but when built and hosted the pages are ...

I'm working on my first GatsbyJS project (and hosted on Netlify) and over the past few weeks have noticed that everything is very speedy during development, but when built and hosted the pages are ...

here i didn't understand what happen when i use var before variable in function it give me different out put and with out using var also i got a different output here is a code that you can easily ...

here i didn't understand what happen when i use var before variable in function it give me different out put and with out using var also i got a different output here is a code that you can easily ...

  1. explain value chain analysis
  2. explain value
  3. explain value added tax
  4. explain value chain
  5. explain value education
  6. explain value engineering with examples
  7. explain value of biodiversity
  8. explain value chain model
  9. explain value analysis
  10. explain value for money
  11. explain value engineering
  12. explain value proposition
  13. explain value added method
  14. explain value stream mapping
  15. explain value addition
  16. explain value at risk
  17. explain value chain analysis with example
  18. explain value based pricing
  19. explain value of information
  20. explain value and address operator in c

I am trying to use velocity together with jQuery (only for IE8 support) in an ES6 module. Consider this code: import { Component } from 'react'; import ReactDOM from 'react-dom'; import jquery from '...

I am trying to use velocity together with jQuery (only for IE8 support) in an ES6 module. Consider this code: import { Component } from 'react'; import ReactDOM from 'react-dom'; import jquery from '...

  1. using velocity to find acceleration
  2. using velocity to find force
  3. using velocity in a sentence
  4. using velocity in unity
  5. using velocity to find angle
  6. using velocity in javascript
  7. using velocity to find drag
  8. using velocity in python
  9. using velocity to find energy
  10. using velocity in angular
  11. using velocity points with singapore airlines
  12. using velocity points with etihad

Firebase Cloud Messaging I have everything setup, the Push messages are received fine and when I click on it, it opens new window... but only in Chrome, in Firefox it is not opened. I have ...

Firebase Cloud Messaging I have everything setup, the Push messages are received fine and when I click on it, it opens new window... but only in Chrome, in Firefox it is not opened. I have ...

  1. firefox window opening off screen
  2. firefox opening small window
  3. firefox opening new window instead of tab
  4. firefox opening private window
  5. firefox opening links in new window
  6. firefox not opening private window
  7. firefox not opening new window
  8. firefox keeps opening new window
  9. firefox disable new window opening
  10. firefox prevent opening new window