Get source url of script tag in Javascript on the iPhone over 3G
I have a javascript widget that is embedded on third party websites. It needs to load other resources (CSS, flash, etc) relative to itself (rather than relative to the page it is hosted on).
To do this I have been looping through all the script tags in the page, finding my script tag, and getting the src
property from it.
e.g.
path = '';
$('script').each(function(i, script) {
var src = $(script).attr('src');
if ('name-of-widgets-file.js' === src.replace(/^.*[\/\\]/g, '')) {
return path = src.substring(0, src.lastIndex开发者_JS百科Of('/'));
}
});
This works on all devices I have tried it on, except for the iPhone on a 3G connection! When connected over 3G the iPhone returns undefined
for the src
attribute of the script tag!
Does anyone have any ideas for working around this? Possibly another way to get the url of the currently executing script tag?
I've found a good work around for this.
Rather than looking for the script after it has run, I now work out the path
while the script is executing. In this case the iPhone returns the src
attribute when on a 3G connection.
var scripts = document.getElementsByTagName('script');
var script = scripts[scripts.length - 1];
var src = script.src;
if (!src) {
return '';
}
return src.substring(0, src.lastIndexOf('/'));
精彩评论