admin管理员组

文章数量:1428327

I am trying to pass a variable in javascript. I create a link in the following manner and everything seems to be working.

label.innerHTML = '<a href="#" onclick="show_box(this);"> link</a>';

However when I create the link in the following way where the link would also pass an associated object I get the following error from firebug -> "missing ] after element list"

label.innerHTML = '<a href="#" onclick="show_box(this,'+object+');"> link</a>';

Is this an acceptable way to pass an object to a function. The problem is that I am creating this link within a function. The function creates links like this based upon an object that is passed to it. Therefore I cannot have this "object" as a global scope.

I am trying to pass a variable in javascript. I create a link in the following manner and everything seems to be working.

label.innerHTML = '<a href="#" onclick="show_box(this);"> link</a>';

However when I create the link in the following way where the link would also pass an associated object I get the following error from firebug -> "missing ] after element list"

label.innerHTML = '<a href="#" onclick="show_box(this,'+object+');"> link</a>';

Is this an acceptable way to pass an object to a function. The problem is that I am creating this link within a function. The function creates links like this based upon an object that is passed to it. Therefore I cannot have this "object" as a global scope.

Share Improve this question asked May 18, 2011 at 17:27 slimboslimbo 2,7594 gold badges26 silver badges36 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

You are building the script by mashing together strings, as such you can only work with strings and object will be automatically stringified.

Use DOM instead.

var link = document.createElement('a');
link.href = "#"; // Have a more sensible fall back for status bar readers and middle clickers
link.appendChild(document.createTextNode(' link');
link.addEventListener('click',function () { show_box(this, object); },false);
label.appendChild(link);

… but use a library that abstracts away the non-standard event models that some browsers have.

What you're trying to do is pass the contents of object to output. Since it's an object, the string representation will be something like [object Object]. The output HTML would look like:

<a href="#" onClick="show_box(this, [object Object]);">link</a>

which is invalid. Don't try to concatenate the object, just pass it along as another argument to the function, like this. Or, better yet, use jQuery:

<!-- somewhere in the head, or at least after the object is defined -->
<script type="text/javascript">
$(function() {
    $('#thelink').click(function() { show_box(this, object); });
});
</script>

...

<a href="#" id="thelink">link</a>

If your object is simple variable like numeric or string variable than it will be Ok but if you are passing html object it will not work because it will be something like below.

    <a href="#" onclick="show_box(this,[object HTMLDivElement]);"> link</a>

本文标签: htmlPassing Variables JavascriptStack Overflow