1

I am new to Javascript. I am writing a Java code which internally write code to call predefined Javascript method.

I have a Javascript method defined as:

  function myfunction(url, params) {
        $.post(url, params);
  }

This send params which is a JSON to the URL as post (I suppose).

My Java code generate HTML on the fly which internally generate something like:

<iframe onLoad="myfunction(myinternal, {"system":"abcdef", "token": "12345"})"> ......

However, this fails to run. Page inspection gave me:

SyntaxError: invalid property id
       myfunction(myinternal, {
                               ^

What did I do wrong here?

Many thanks

2
  • 4
    You are ending the attribute with your second ". Either wrap the attribute in ' or use javascript to capture the onload event. Commented Jul 8, 2013 at 15:39
  • Also note that what you are calling "a JSON" is in fact a Javascript object literal. Commented Jul 8, 2013 at 15:41

3 Answers 3

8

Your HTML has syntax errors. Should be more like this:

<iframe onLoad="myfunction(myinternal, {\"system\":\"abcdef\", \"token\": \"12345\"})">

or wrap in a single quote:

<iframe onLoad='myfunction(myinternal, {"system":"abcdef", "token": "12345"})'>

EDIT: (per first comment)

<iframe onLoad="myfunction(myinternal, {&quot;system&quot;:&quot;abcdef&quot;, &quot;token&quot;: &quot;12345&quot;})">
Sign up to request clarification or add additional context in comments.

2 Comments

The first solution is not correct, html does not allow escaping like that. You should use &quot; in that case.
Thanks @RenéWolferink, I will edit the solution to eliminate confusion.
2

You need to escape your quotes inside the json object.

Comments

1

You used double quotes " in your JSON. They are interpreted by the HTML parser as the end of the onLoad attribute.

Try with single quotes, like this :

<iframe onLoad="myfunction(myinternal, {'system':'abcdef', 'token': '12345'})">

Comments

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.