Inlocuire date cu javascript in src iframe

Discutii si intrebari legate de scripturi si functii JavaScript, jQuery si Ajax, cod JavaScript in general.
MelecaCristian
Mesaje: 176

Inlocuire date cu javascript in src iframe

Buna am o mica problema ... am in site scrise coduri iframe gen :

Cod: Selectaţi tot

<iframe src="http://vk.com/video_ext.php?oid=97788243&id=162602594&hash=6c319dd471bbeabf&hd=1" width="607" height="360" frameborder="0"></iframe>
Aparent nici o problema dar vreau sa adaug dupa fiecare &hd=1 inca o variabila ci anume &autoplay=true , am tot cautat de ceva vreme pe net si am gasit ca se pot inlocui cuvinte cu javascript (de asta am si nevoie de js nu php) dar nu stiu cum sa fac sa schimbe in pagina, nu doar in cod js gen cel de jos .

Foloseste metoda JavaScript replace() si mam gandit ca e buna pentru a inlocui hd=1 cu autoplay=true

Cod: Selectaţi tot

<p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>
<p id="demo">Visit Microsoft!</p>
<button onclick="myFunction()">Try it</button>
<script type="text/javascript">
function myFunction(){
var str=document.getElementById("demo").innerHTML; 
var n=str.replace("Microsoft","W3Schools");
document.getElementById("demo").innerHTML=n;
}
</script>
va rog ajutor :( multumesc
„Uneori o greşeală poate fi tot ce este necesar pentru o realizare valoroasă.” — Henry Ford

MarPlo Mesaje: 4343
Salut,
Daca vrei sa inlocuiesti "hd=1" cu " autoplay=true " in adresa SRC de la iframe, poti folosi exemplu din acest script:

Cod: Selectaţi tot

<iframe src="http://vk.com/video_ext.php?oid=97788243&id=162602594&hash=6c319dd471bbeabf&hd=1" width="607" height="360" frameborder="0" id="ifr1"></iframe>

<script type="text/javascript"><!--
var srcifr = document.getElementById('ifr1').src.replace('hd=1', 'autoplay=true');
document.getElementById('ifr1').src = srcifr;
--></script>
Daca vrei sa ramana si "hd=1", iar "&autoplay=true" sa fie dupa el, folosesti replace() asa: replace('hd=1', 'hd=1&autoplay=true'); .

MelecaCristian Mesaje: 176

Cod: Selectaţi tot

<script type="text/javascript">
if (document.documentElement && document.documentElement.childNodes)
window.onload = function() {
var tn = [], grabTextNodes = function(n) {
for (var i = n.length - 1; i > -1; --i)
if (n[i].nodeName != '#text' && n[i].childNodes)
grabTextNodes(n[i].childNodes);
else
tn[tn.length] = n[i];
}, replacements = {
    "&hd=1"     : "&hd=1&autoplay=true"
    
    }, regex = {};

grabTextNodes(document.body.childNodes);

for (var key in replacements) {
regex[key] = new RegExp(key, 'g');
for (var i = tn.length - 1; i > -1; --i)
tn[i].nodeValue = tn[i].nodeValue.replace(regex[key], replacements[key]);
};
};
</script>
am gasit si eu acest cod dar nu face ce vreau eu :( pentru ca eu nu am idul acela care lai precizat tu

Eu il am asa simplu :

Cod: Selectaţi tot

<iframe src="http://vk.com/video_ext.php?oid=97788243&id=162602594&hash=6c319dd471bbeabf&hd=1" width="607" height="360" frameborder="0"></iframe>


si problema cu scripul gasit de mine e ca :

daca nu este in <iframe src... imi schimba dar daca este in <iframe src... nu imi schimba cum pot modifica sau inlocui acest lucru ?
„Uneori o greşeală poate fi tot ce este necesar pentru o realizare valoroasă.” — Henry Ford

MarPlo Mesaje: 4343
Incearca codul de mai jos.
Data: "autoplay=true" se adauga in adresa SRC la toate elementele <iframe> din pagina (care sunt adaugate inainte de script-ul JavaScript.

Cod: Selectaţi tot

<iframe src="http://vk.com/video_ext.php?oid=97788243&id=162602594&hash=6c319dd471bbeabf&hd=1" width="607" height="360" frameborder="0"></iframe>

<script type="text/javascript"><!--
var ifrms = document.getElementsByTagName('iframe');
var nrifrms = ifrms.length;
for(var i=0; i<nrifrms; i++) {
  var srcifr = ifrms[i].src + '&autoplay=true';
  ifrms[i].src = srcifr;
}
--></script>

Subiecte similare