0

I was trying to include

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">

I need to edit the src path dynamically. That is, I need to change the protocol(http/https) dynamically. How can I write the src which calls the url like, location.protocol+ajax.googleapis......

2 Answers 2

3

There's no need to do this dynamically - you can just say:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">

Starting the src with the double-slash means "use whatever protocol the page is using."

Sign up to request clarification or add additional context in comments.

Comments

1

You can embed a script dynamically:

var src = 'myurl';
if (something) {
    src = 'other';
}
var script = document.createElement('script');
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.