Raphael
Raphael

Reputation: 703

JQuery: how to change urls of a iframe if I come from http:// to https://

I've a little question. I wanna change the url of a IFrame if the customer login and has the url prefix https://

So before login the url of the iframe is e.g. http://www.google.de/pictures/iframe.html and after he logged in the url of the iframe is https://www.google.de/pictures/iframe.html

The differens is only the "https"

Upvotes: 2

Views: 1595

Answers (3)

Mathias Bynens
Mathias Bynens

Reputation: 149524

You don’t need JavaScript to do this. All you need is HTML:

<iframe src="//www.google.de/pictures/iframe.html" id="ifrm1">

See how I omitted the http: or https: part? That’s a scheme-relative URL.

With this code, the iframe will use http: or https: depending on whatever scheme/protocol the outer document is using.

Upvotes: 4

dknaack
dknaack

Reputation: 60448

Here is the solution:

<img src="http://yourImage.com/image.gif" id="pct1" />

<script>
    $().ready(function () {
        prefix = parent.location.protocol;
        if (prefix == "http:") {
            // change to http
            $('img#pct1').attr("src", $('img#pct1').attr("src").replace("https://", "http://"));
        }
        else {
            // change to https
            $('img#pct1').attr("src", $('img#pct1').attr("src").replace("http://", "https://"));
        }
    });
</script>

Upvotes: 3

dknaack
dknaack

Reputation: 60448

If the image is embedded in your site you should use relative path to the image and switch the site over to https.

Anyway, you can change the image source using jQuery this way.

$("#yourImageTagIdOrOtherSelector").attr('src','https://www.google.de/pictures/picture1.jpg'); 

Upvotes: 0

Related Questions