copenndthagen
copenndthagen

Reputation: 50800

enforce http with JavaScript

I have a srcURL variable which gets a path of the form /myFolder/myFile.jpg

Now this gets assigned to the img element..which obviously would call it with the complete path https://mySite.com/myFolder/myFile.jpg

Now I somehow want the https to be replaced/enforced with http using Javascript..

I am not sure if I can do this with the "replace()" method since I only get the path "/myFolder/myFile.jpg" in the srcURL variable and not with https..

How can I do that?

Upvotes: 0

Views: 231

Answers (2)

vol7ron
vol7ron

Reputation: 42179

You are using a relative path. You need to use an explicit path when setting the src of the URL.

srcURL = '/myFolder/myFile.jpg';
srcURL = 'http://' + window.location.host + srcURL;

// srcURL == 'http://<yourdomainname>/myFolder/myFile.jpg'

Note: you'll probably get a warning message saying some parts of your page may be unsecure.

Upvotes: 3

Mike Thomsen
Mike Thomsen

Reputation: 37526

If you want to enforce plain HTTP, you should write a rewrite rule on the server to forward any HTTPS request for an image to the HTTP equivalent. On the client side, simply doing this would be sufficient (but you really need the back end piece too):

url.replace("https", "http");

Upvotes: 1

Related Questions