2

My hypertlink generated by JavaScript is as follows:

'<a href="#" onClick=passSearchTerm("' +aa + '");>View</a>' 

The parameter passes wells when the string is as follows:

var aa = 'TAGS(\\"ab\\")'; 

I confirmed it by alerting inside the function

function passSearchTerm(aa) { alert(aa); }

However, this wont alert when the var is defined as follows:

var aa = 'TAGS(\"a b\")'; //space between "a" and "b"

Can anyone suggest me how to pass such string? Thanks in advance for your help. Actually I am trying to pass likely following string:

TAGS("a or b")

6
  • 2
    Could you reproduce this in a jsFiddle? Commented Jan 9, 2013 at 9:39
  • 1
    What does your method TAGS do? Commented Jan 9, 2013 at 9:41
  • TAGS too is part of String i want to pass, like consider another sting instead var a='apple(\"a or b\')' Commented Jan 9, 2013 at 9:46
  • @THiCE , tags is aslo a part of string Commented Jan 9, 2013 at 9:53
  • @THiCE: What TAGS() returns is irrelevant. The problem here is how to pass the string "TAGS("a or b")" to a inline onclick. Commented Jan 9, 2013 at 9:54

3 Answers 3

5

While generating ur hyperlink by javascript use encodeURIComponent ex.

"<a href=\"#\" onclick=\"passSearchTerm('" + encodeURIComponent(aa) + "')\">View</a>"

and while retriving use decodeURIComponent

function passSearchTerm(aa) { alert(decodeURIComponent(aa)); }
Sign up to request clarification or add additional context in comments.

1 Comment

thank you! encodeURIComponent did the trick for me... I was going crazy with this problem and I cannot make the code actually good because I'm just doing maintenace for a web app and I just needed to fix this little but annoying bug.
3

I think you've just overused your escapes.

This works for me.

  var aa = 'TAGS("a b")'; 
  // var aa =  'TAGS(\"a b\")'; 

  var e = document.createElement("A");
  e.href = "#";
  e.setAttribute( "onClick", "passSearchTerm('" + aa + "');" );
  e.innerText = "View";

  document.body.appendChild(e);

  function passSearchTerm(p){
    alert(p);
  }

</script>

Comments

0
var aa = 'TAGS(\\"a&nbsp;b\\")';

This code does work. It is because in html space should written as &nbsp;

[update]

Here is the full example. http://jsfiddle.net/YAJNL/

3 Comments

@Adrian: You should use it with the code provided by the questioner. jsfiddle.net/YAJNL here is the full code example.
You mean the code with a mistake in it? The mistake is incorrectly escaping the various nested " or ' characters. To say you should use a No-Break Space, here looks wrong. Fixing the Escapes is much more useful and 'looks' some much better. If you fix the escapes, the code then 'works' with either a regular space or &nbsp; ... Although I would still maintain that its correct to use a regular space, as subsequent parsing of the string may stumble on &nbsp; . a Space is encoded as /u0020 and &nbsp; as /u00A0 internally.

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.