1

I'm trying to pass a string like this:

{"key":["value"],"key2":undefined,"key3":undefined,"key4":undefined,"key5":"value"}

to a javascript-function like this:

<a href="#" onClick="myFunction(myString);">

but can't get the escaping right. Is there a way to pass that object-string to a function or do I need to convert something?

Greetings, Select0r

4 Answers 4

1

try:

var myString = '{"key":["value"],"key2":undefined,"key3":undefined,"key4":undefined,"key5":"value"}';


EDIT:

In light of your recent comment I went back to the browser and tried this (works for me):

<a href="#" onClick="myFunction({'key':['value'],'key2':undefined,'key3':undefined,'key4':undefined,'key5':'value'});">

The change means that it's no longer longer passed as a string but as an object parameter to myFunction.

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

2 Comments

I tried that before and it didn't work: I get an "unterminated string literal" error. The problem is that Javascript won't be executed in that context (special Dojo-template, IE6 ignores Javascript there) and as all Javascript needs to go inside the "onclick", there are already enclosing quotes that cause the error ...
Thanks, I guess that would work for me if I could only perform a str.replace on myString before passing it to myFunction. myString is passed through an AJAX-request and I can't change the result so I'm stuck with myString as it is, so my only chance is probably a different approach in generating the data in the first place ...
1

As Naeem said, you can enclose the string in a single quote. The difference between the single and double quote is this:

single quotes:

  • Can contain double quotes without stopping string
  • Cannot contain characters such as break lines
  • Can contain single quotes via \'

double quotes:

  • Can contain single quotes without stopping string
  • Can contain break line and other special characters
  • Can contain double quotes via \"

1 Comment

I guess I just have too many quotes :) The code will look something like: onclick="myFunction('{key"... while the last quotes belongs to the string and terminates the onclick-quote. As I commented above: in this special context, all Javascript needs to be inside the onclick - right now I have no idea, if that's possible at all.
1

I found a solution, Naeem Sarfraz put me on the right track - it's not going to win a beauty contest, but it works:

As I can execute PHP in the context I'm in (but IE6 would ignore Javascript), I did a couple of replacements on single/double quotes with PHP.

$data = stripslashes(unserialize($data));
$data = addcslashes($data, "'");
$data = str_replace('"', "'", $data);

This will strip all slashes, add slashes for single quotes only and finally replace double quotes with single ones.

Now myString is in a state that can be passed to a Javascript function in onclick without quote-conflicts:

<a href="#" onClick="myFunction(<?php print $data; ?>);">

Thanks for your contributions!

2 Comments

One trick that I've used in the past is to put a JSON string in an invisible element. You can then use onclick="myFunction(eval($('#myDiv').html()))"... I'm not sure if this makes it any easier or harder for your implementation though (also, the example does require jQuery).
I guess I stick with my replacements for now, but I'll keep that in mind, thanks. I don't work with jQuery but with Dojo so an implementation similar to yours should be no big deal.
0

If you can generate code just before this <a> element, you can try this:

<script type="text/javascript">
var myObj = {"key":["value"], "key2":undefined, "key3":undefined, "key4":undefined, "key5":"value"};
</script>
<a href="#" onClick="myFunction(myObj)">

1 Comment

IE6 ignores Javascript in this context completely, so I can't go with this solution.

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.