Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the iframe src according to the url?

Tags:

html

php

iframe

An iframe is found with some different pages forexample <iframe src="..."></iframe> in site.com/page1 and site.com/page2, I want to change the iframe src according to the url above, Can I do that, If I can how can I do that? Please help!

Note: the url is dynamic so I can't use if else condition

Thanks

like image 503
Miheretab Alemu Avatar asked Mar 20 '23 07:03

Miheretab Alemu


2 Answers

Yes.

The simplest solution is don't set the iframes src in the HTML, just declare the iframe tag and give it an id, say "myIframe".

Then just do some simple javascript.

var iframe = document.getElementById('myIframe');
if(window.location.href === SITE_1_URL){
   iframe.src = something;
} else {
   iframe.src = somethingElse;
}

Note the iframe doesn't load until its src property is set.

like image 104
asutherland Avatar answered Mar 22 '23 21:03

asutherland


on page ready event you check the current url and set the iframe src attribute according see this link

like image 23
Sérgio S. Filho Avatar answered Mar 22 '23 21:03

Sérgio S. Filho