1

I have a function which returns a string which contains a javascript call. Since it is quoted I cannot pass variables through it.

How can I change the return string which would enable me to pass javascript object values.

Ex: How can I pass var i through the return statement.

var i = 'iaa';

return '<a href="javascript:abccd(\'i\');" ><img src="../images/btnsave2.png" style="margin:6px 0 0 6px;" height="13" width="13" /></a> ';

3
  • 7
    Eugh. Refactor. Use standard DOM instead of passing chunks of HTML around. Then you can use standard event handler binding techniques. Commented Jan 10, 2012 at 14:02
  • Why is the alt attribute missing from that image? Commented Jan 10, 2012 at 14:12
  • @Quentin :- I'm using this inside a custom formatter in JQGrid. I don't think there's anyway I can pass standard DOM around Commented Jan 10, 2012 at 16:05

2 Answers 2

2

If you need to manipulate raw HTML, at least add the event handler separately, in JavaScript. And styles in a CSS file.

Your image tag code should contain a data- attribute:

i = i.replace(/"/g, '\\"')
return '<img data-input="' + i + '" src="../images/btnsave2.png" />'

Then adding a listener is as simple as that:

var onClick = function (e) {
    var data = e.target.getAttribute('data-input')
    if (data) {
        abccd(data)
    }
}

/* Firefox, Chrome &c */
if (document.addEventListener) {
    document.addEventListener('click', onClick, false)
/* IE <9 */
} else if (document.attachEvent) {
    document.attachEvent('onclick', function () {
        return onClick({ target: event.srcElement })
    })
}

Of course, XSS problems, mentioned by @Quentin, are still a concern. But at least you won't have to escape nested quotes in the inline handler as in your original example.

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

2 Comments

document.body.addEventListener is not working in IE. I tried using this stackoverflow.com/questions/1695376/… also but it's only working when I have e.target.getAttribute('data-input')
@Nipuna Silva, I'm not sure what exactly your problem with attachEvent is, but see the updated answer.
2

Try this, but please reconsider your actions before putting into practice.

var i = "halo";
return '<a href="javascript:abccd(\''+i+'\');" ><img src="../images/btnsave2.png" style="margin:6px 0 0 6px;" height="13" width="13" /></a> ';

3 Comments

What's the bad practice in this?
This will break if i contains a double quote, backslash, new line (and possibly some other characters).
The first bad experiance doing this is like Quentin said the special characters in the passed variable. The second thing is that you could do some more reliable reusable sustainable code not just a "it works so it's ok" code. One of the ways to accomplish what you want in a more suitable way is katspaugh 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.