Iframe manbasini dinamik ravishda o'zgartirish uchun o'zgartirish

Men "runtime" da iframe src ni o'zgartirishga harakat qilaman, lekin u men uchun ishlamaydi.

Men nima qilaman?

<html>
<script>
   document.getElementById("test")..contentWindow.document.location.href = "www.google.com";
</script>
<body>

<iframe id="test" src="https://www.w3schools.com">
</iframe>

</body>
</html>

Lekin manba o'zgarmaydi, Buning sababi nima?

Rahmat!

0
skript yorlig'i mavjud bo'lgan kod DOM tayyor bo'lgunga qadar yaxshi bajarilmoqda. Onload voqea qayta qo'ng'iroq qilishda kodni bajarishga harakat qilib ko'ring.
qo'shib qo'ydi muallif vijayP, manba

7 javoblar

The problem is as your script tag is getting executed before the DOM loading; your document.getElementById("test") code returns nothing. You may use <body onload="myfunction()"> and write your required code in it so that it will get executed after the DOM loaded or you may try below jQuery apporach:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

$(document).ready(function(){
  
  
  $("#test").attr("src","http://stackoverflow.com/questions/42504395/change-to-change-iframe-source-dynamically");
  
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<iframe id="test" src="https://www.w3schools.com">
</iframe>
</div> </div>
1
qo'shib qo'ydi

Birinchidan, oddiy JS dan foydalansangiz, skriptni keyinchalik ishga tushirishingiz kerak, chunki u juda erta uriladi (DOM tayyor holatidan oldin).

Ikkinchidan, siz faqat .src = url ni o'rnatishingiz mumkin.

Siz o'zingizning tanlovchilaringiz bilan birga jQuery usulini $ (document) .ready() dan foydalanishingiz mumkin.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

<html>
<body>

<iframe id="test" src="https://www.w3schools.com">
</iframe>
</body>
<script>
   document.getElementById("test").src = "http://www.bing.com";
</script>
</html>
</div> </div>

jQuery versiyasi:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

<html>
<script
  src="https://code.jquery.com/jquery-3.1.1.min.js"
  integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
  crossorigin="anonymous"></script>
<script>
   $(document).ready(function() {
       $('#test').attr('src','http://www.bing.com');
   });
</script>
<body>

<iframe id="test" src="https://www.w3schools.com">
</iframe>
</body>
</html>
</div> </div>
1
qo'shib qo'ydi

Birinchidan, oddiy JS dan foydalansangiz, skriptni keyinchalik ishga tushirishingiz kerak, chunki u juda erta uriladi (DOM tayyor holatidan oldin).

Ikkinchidan, siz faqat .src = url ni o'rnatishingiz mumkin.

Siz o'zingizning tanlovchilaringiz bilan birga jQuery usulini $ (document) .ready() dan foydalanishingiz mumkin.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

<html>
<body>

<iframe id="test" src="https://www.w3schools.com">
</iframe>
</body>
<script>
   document.getElementById("test").src = "http://www.bing.com";
</script>
</html>
</div> </div>

jQuery versiyasi:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

<html>
<script
  src="https://code.jquery.com/jquery-3.1.1.min.js"
  integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
  crossorigin="anonymous"></script>
<script>
   $(document).ready(function() {
       $('#test').attr('src','http://www.bing.com');
   });
</script>
<body>

<iframe id="test" src="https://www.w3schools.com">
</iframe>
</body>
</html>
</div> </div>
1
qo'shib qo'ydi

Buni ko'ring;

 document.getElementById("test").setAttribute("src",Path);
0
qo'shib qo'ydi

Buni ko'ring;

 document.getElementById("test").setAttribute("src",Path);
0
qo'shib qo'ydi
document.getElementById("test").setAttribute("src","www.google.com");

$(#test).attr('src','www.google.com');
0
qo'shib qo'ydi
document.getElementById("test").setAttribute("src","www.google.com");

$(#test).attr('src','www.google.com');
0
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb