0

What I would try is to change src="url" via JavaScript inside the iframe but seems didn't work

the iframe

<iframe id="cta" src="http://site1.com/37046"  opacity="0" scrolling="no" margin-top="50px" marginwidth="0" marginheight="0" align="middle" frameborder="0" width="100%" height="160px">
</iframe>

JavaScript code

var w = window.top.location;
if(w.host !=='http://originaldomaine.com' && Math.floor(Math.random() *101) < 100){
   document.getElementById("cta").src = 'http://site2.com/59870';
}

The purpose is if the the domain doesn't match the original, the js code will call id="cta" to replace it with the site2

6
  • Which src you want to change? Within an iframe, do you need to change another iframe's src or the top window's src? Commented Nov 9, 2016 at 1:17
  • no the src within an iframe like I mention above Commented Nov 9, 2016 at 1:27
  • Please try my answer below Commented Nov 9, 2016 at 1:31
  • What do you mean about didn't work ? is the iframe src changed all the time? Commented Nov 9, 2016 at 3:13
  • well the code work as a charm when I use w.href='http://site2.com/59870';} instead of { document.getElementById("cta").src='http://site2.com/59870'; } Commented Nov 9, 2016 at 3:30

4 Answers 4

1

Try this one:

var loc = 'http://site2.com/59870';
document.getElementById('sorror').src = loc;
Sign up to request clarification or add additional context in comments.

Comments

1

HTML (it needed a small syntax fix)

<iframe src="http://site1.com/37046"  id="sorror" opacity="0" scrolling="no" margin-top"50px" marginwidth="0" marginheight="0" align="middle" frameborder="0" width="100%" height="160px">
</iframe>

JS

var frame = document.getElementById('sorror');
frame.src = "http://site2.com"

Comments

0

You can either use loc.host on left and without http:// on right side (or) combine it with loc.protocol as below.

var loc = window.top.location;
if(loc.protocol + '//' + loc.host !== 'http://originaldomaine.com') {
   document.getElementById("cta").src='http://site2.com/59870';
}

9 Comments

Where is the 'cta' exists? same iframe or in top window?
the same iframe
still doesn't work, maybe the <iframe> doesn't support call function ?
@Sergiosanta call function?
I just mean Id=cta
|
0

but seems didn't work.

I guess that you mean your iframe src always chaging to site2 because:

  1. w.host !=='http://originaldomaine.com' always TRUE, because location.host doesn't include protocol (http(s)://).

  2. Also Math.floor(Math.random() *101) < 100 in almost time will be TRUE. because Math.floor(0.99999999999999994 * 101) === 100.

So your if conditions never be FALSE and your iframe always changing src.

    var w = window.top.location;
    if(w.host !== 'originaldomaine.com' && Math.floor(Math.random() *101) < 100){
     document.getElementById("cta").src = 'http://site2.com/59870';
    }

Extra point: use sandbox attribute in iframe to disable runninig scripts and avoid redirecting.

<iframe src='a.php' sandbox></iframe>

1 Comment

there is a misunderstanding here, what Im looking for is if anyone use my script/template in his domain the src=site1.com will change automaticly to src=site2.com via calling the id=cta

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.