0

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

I have those all 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?

Community
  • 1
  • 1
lamp_scaler
  • 787
  • 2
  • 10
  • 18
  • You shouldn't have any problems with cross-domain js sources. What does your code look like? Does it work if you include a script tag in the HTML instead of calling .getScript()? – Ricardo Tomasi Aug 26 '11 at 05:24

1 Answers1

1

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.

Stephen
  • 5,362
  • 1
  • 22
  • 33
  • I'm using the getScript() to get around cross-domain issues. The getScript() is actually called within an ajax() success option after a DOM snippet is loaded into our page. That partial DOM is operated on by the script that is loaded by getScript(). – lamp_scaler Aug 26 '11 at 06:45
  • 1
    Ah, looks like I needed a little more info. Unfortunately my only recommendation would be to store that data in a global variable, then wrap whatever is in that js file in a function that immediately uses that global variable and call it in vai that 'addScript'. It's not flexible and it's not pretty.. but it's about as far as I can help. Good luck! – Stephen Aug 26 '11 at 07:55
  • Thanks, Stephen. Ended up using the global variable. You can put that as your answer and I can pick it! – lamp_scaler Oct 08 '11 at 04:07