Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify iframe.src without entry to history object

I have an iframe in my web page. I modify the src property via javascript like so:

document.getElementById('myiframe').src = 'http://vimeo.com/videoid1';
document.getElementById('myiframe').src = 'http://vimeo.com/videoid2';
document.getElementById('myiframe').src = 'http://vimeo.com/videoid3';

However, everytime I do this, it's logged into the browser's history. So everytime I press back in the browser window, the iframe content goes from videoid3 to videoid2 to videoid1. If I press back again, the entire page goes back.

I would like to modify the iframe src with javascript WITHOUT logging an entry into the browser's history. So if i click the browser back button, the entire page goes back without updating the iframe.

I tried doing something like:

document.getElementById('myiframe').contentWindow.location.replace('http://vimeo.com/videoid1');
document.getElementById('myiframe').contentWindow.location.replace('http://vimeo.com/videoid2');
document.getElementById('myiframe').contentWindow.location.replace('http://vimeo.com/videoid3');

Although this made the browser back button behave the way I wanted to, it broke certain things in the vimeo video. Vimeo REQUIRES you to change urls via the iframe.src instead of contentWindow.location.replace().

As such, how do I modify the iframe.src WITHOUT logging into history?

Related This is actually one of the solutions I'm exploring to solve the main problem, which I posted here History object back button with iframes

like image 541
John Avatar asked Feb 06 '13 19:02

John


People also ask

How can I change src attribute in iframe?

To change the iframe src attribute, we will be using a select drop down menu, and a button element to activate the function that will change the src attribute. Note that this is only for example purposes, instead of using onClick you may want to consider using event handlers in an external script.

Can SRC property of an iframe can be changed using JavaScript?

To navigate URL in iframe with JavaScript, we have to set the src attribute or return the value of src attribute in an iframe element. The src attribute defines the URL of document that can be shown in an iframe.

What is iframe src attribute?

The HTML <iframe> src attribute is used to specify the URL of the document that are embedded to the <iframe> element. Syntax: <iframe src="URL"> Attribute Values: It contains single value URL which specifies the URL of the document that is embedded to the iframe.


1 Answers

don't change the src, just replace the old iframe with a new one?

const urls = [
  "http://bing.com",
  "http://google.com",
  "http://duckduckgo.com"
];

function next() {
  if(urls.length==0) return;
  const original = document.getElementsByTagName("iframe")[0];
  const newFrame = document.createElement("iframe");
  newFrame.src = urls.pop();
  original.parentNode.replaceChild(newFrame, original);
}

nextbtn.addEventListener("click", () => next());
iframe {
  width: 300px;
  height:300px;
}
<p>let's test some iframes</p>
<button id="nextbtn">next</button>
  <iframe />

No history states. Same functionality. Everyone wins.

like image 148
Mike 'Pomax' Kamermans Avatar answered Sep 26 '22 23:09

Mike 'Pomax' Kamermans