开发者

little javascript syntax help needed (short question)

开发者 https://www.devze.com 2023-02-04 20:46 出处:网络
I have an external js file which I am creating a tag in. Problem is I don\'t know how to use the quotes if I am to add an onclick event to the code below.

I have an external js file which I am creating a tag in.

Problem is I don't know how to use the quotes if I am to add an onclick event to the code below.

Now this code works:

banner_div.innerHTML = '<a href=\"开发者_如何转开发'+links[counter]+'\"><img src=\"'+images[counter]+'\" border=\"1px\" style=\"border-color:#000;\" alt=\"'+alts[counter]+'\" title=\"'+titles[counter]+'\"></a>';

But I need to add this to the above:

onclick="_gaq.push(['_trackEvent', 'Link Clicks', 'From Index.html', 'www.domain.com']);"

How should I insert the onclick event to the first piece of code so it works?

Thanks

BTW: The arrays above are iterated inside a for loop. So for example links[counter] contains the current index of the links array, which could be for example "www.somedomain.com".


Looks like a problem with using single quotes inside a single-quote delimited string. Remember, the whole onclick= thing is inside the large string surrounded by single quotes. So just backslash-escape the single quotes:

onclick="_gaq.push([\'_trackEvent\', \'Link Clicks\', \'From Index.html\', \'www.domain.com\']);"

I would avoid using innerHTML, especially if you have a lot of substitutions. Too much can go wrong. For example, what if alts[counter] has some special HTML characters such as &? I would use the DOM to manipulate the object instead. For example

a = document.createElement("a");
a.href = links[counter];
a.setAttribute("onclick", "_gaq.push(['_trackEvent', 'Link Clicks', 'From Index.html', 'www.domain.com']);")
img = document.createElement("img");
img.src = images[counter];
img.border = "1px";
img.style = "border-color:#000;";
img.alt = alts[counter];
img.title = titles[counter];
a.appendChild(img);
banner_div.appendChild(a);

Though I guessed that without testing it, so it might be a bit wrong.

Note that I used short-hand property notation to assign all the attributes, but I used setAttribute to assign onclick because I tried using the property notation and it didn't work for me (the DOM HTML specification allows you to set most properties directly, but not onclick).


You don't have to escape double-quotes in a single-quoted string (and vice versa), so first we can simplify this quite a lot by just alternating quote-type appropriately:

banner_div.innerHTML = '<a onclick="' + "_gaq.push(['_trackEvent', 'Link Clicks', 'From Index.html', 'www.domain.com']);" + '" href="' + links[counter] + '"><img src="' + images[counter] + '" border="1px" style="border-color:#000;" alt="' + alts[counter] + '" title="' + titles[counter] + '"></a>';

In my opinion, this is a simpler solution than doing the DOM-manipulation in the accepted answer. We don't have to escape a single thing and we're not using a single function except good old string concatenation.

Why do we have two kinds of quotes? In order to make scenarios with lots of quotes (like this) simple!

0

精彩评论

暂无评论...
验证码 换一张
取 消