Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
51
rated 0 times [  56] [ 5]  / answers: 1 / hits: 42309  / 14 Years ago, fri, january 7, 2011, 12:00:00

Below is the code for my YWA wrapper



var astr_ywascript = (document.createElement(script).type = text/javascript).src = http://d.yimg.com/mi/eu/ywa.js;
document.head.appendChild(astr_ywascript); // <- error on this line


It's run on page load, so it makes no sense that JS can't find the document head tag.



Any ideas?



Thanks






Opera throws this error on the same line. Uncaught exception: Error: WRONG_ARGUMENTS_ER
Firebug says: document.head is undefined [Break On This Error] document.head.appendChild(astr_ywascript);


More From » onload

 Answers
38

In the line



(document.createElement(script).type = text/javascript).src


you are setting the src property of a string. The assignment in parentheses returns the assigned value. You make the same mistake later in the line, ultimately assigning http://d.yimg.com/mi/eu/ywa.js to astr_ywascript



Split it out onto separate lines:



var el=document.createElement(script);
el.type=text/javascript
el.src=...
document.head.appendChild(el);


Raw Javascript rarely behaves in the fluid way one gets used to with jQuery.



You might also want to get the head as follows:



document.getElementsByTagName(head)[0]

[#94340] Wednesday, January 5, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
karivictoriab

Total Points: 530
Total Questions: 90
Total Answers: 95

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;