开发者

Setting new absolute path of resources within Javascript depending on environment

开发者 https://www.devze.com 2023-03-30 17:33 出处:网络
I have an external javascript file that uses the getScript() function to run another JS file. I have those all 开发者_Python百科on static.mydomain.com. (I\'m new to setting up CDNs)

I have an external javascript file that uses the getScript() function to run another JS file.

I have those all 开发者_Python百科on static.mydomain.com. (I'm new to setting up CDNs)

getScript() doesn't seem to allow cross-domain requests because my HTML is on domain.com. But then I tried using relative paths according to this post: Dynamic URLs in CSS/JS

It works for CSS but does not work for JS (specifically within the getScript() function). What's going on here? What are some ways to mitigate this problem when dealing with CDNs?


The getScript method actually makes an ajax call, hence the reason it's not working. Unless you need access to things like 'was the script successfully found' and the like, it's better to just write up a quick method like...

function addScript(source, domain) {
    $("head").append("<script src='"+ (domain ? domain + source : source) +"'></script>");
}

That will just add scripts to the head of the page, and let you add an optional domain to point to in case you want to change it up.

0

精彩评论

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