Problem with dynamically including CSS from within bookmarklet
Im trying to build a bookmarklet (like http://erkie.github.com/). What i need to do is include a jsfile and in that jsfile i will include another jsfile and a CSS file.
I have the following in the bookmark:
javascript:var script = document.createElement('script');script.id='invoketest';script.type='text/javascript';script.src='http://mydomain.com/start.js';document.getElementsByTagName("head")[0].appendChild(script);void(0);
This will include my start.js file and this works just fine. Now my start.js file looks like this:
var script2 = document.createElement('script');
script2.type = 'text/javascript';
script2.src = 'http://mydomain.com/someother.js';
document.getElementsByTagName("head")[0].appendChild(script2);
var script1 = docum开发者_开发百科ent.createElement('link');
script1.rel = 'stylesheet';
script1.type = 'text/css';
script1.href = 'http://mydomain.com/someCSS.css';
document.getElementsByTagName("head")[0].appendChild(script1);
This kind of work, as the "someother.js" script is loaded just fine. But the "someCSS.css" is NOT loaded for some reason.
Im testing in chrome, but it does not work in IE and FF either.
Hope this makes sense
BR/Sune
It turned out that adding
void(script1);
after the script1 block and same for script2 helped. Now the CSS is loaded just fine.
BR/Sune
精彩评论