admin管理员组文章数量:1400336
$("#div").append('<iframe width="200" height="100" src=";amp;showinfo=0&enablejsapi=1" frameborder="0" allowfullscreen=""></iframe>');
By using this example append
cause page reload, maybe are some issue to load youtube iframe without reloading page
Demo: /
My observation is wrong it's not realod the page.
$("#div").append('<iframe width="200" height="100" src="https://www.youtube./embed/zoMYU_nOGNg?rel=0&showinfo=0&enablejsapi=1" frameborder="0" allowfullscreen=""></iframe>');
By using this example append
cause page reload, maybe are some issue to load youtube iframe without reloading page
Demo: https://jsfiddle/0uqtn0u5/
My observation is wrong it's not realod the page.
Share Improve this question edited May 4, 2016 at 8:42 Wizard asked May 4, 2016 at 8:09 WizardWizard 11.3k38 gold badges99 silver badges167 bronze badges 9- 1 What triggers the call? – mplungjan Commented May 4, 2016 at 8:14
- Your posted code doesn't reload the page but the iframe. Is it your issue??? – A. Wolff Commented May 4, 2016 at 8:15
- @A.Wolff page reloads - same as press F5 – Wizard Commented May 4, 2016 at 8:16
- So something else refreshs it or your observation is wrong. If you think all page is refreshed because of browser loading icon is animated, that not means all the page is refreshed but in this case just that some page element is loading – A. Wolff Commented May 4, 2016 at 8:17
- 1 @Wizard So ya, your observation is wrong, see: jsfiddle/0uqtn0u5/1 – A. Wolff Commented May 4, 2016 at 8:24
3 Answers
Reset to default 3Some browsers will treat <button>
as a submit button and submit to the same page regardless of the button being a child of a form or not.
Change to
$('button').on('click', function(e){
e.preventDefault();
$("#youtube").append('<iframe width="200" height="100" src="https://www.youtube./embed/zoMYU_nOGNg?rel=0&showinfo=0&enablejsapi=1" frameborder="0" allowfullscreen=""></iframe>');
});
or add type="button"
to it.
If you DO have a form, instead do make the button a submit button and prevent the default action on the form submit:
$('form').on('submit', function(e){
e.preventDefault();
$("#youtube").append('<iframe width="200" height="100" src="https://www.youtube./embed/zoMYU_nOGNg?rel=0&showinfo=0&enablejsapi=1" frameborder="0" allowfullscreen=""></iframe>');
});
There is no other reason for the page to reload. Your code should not and indeed does not reload anything in Chrome
Take a look: How to prevent an iframe from reloading when moving it in the DOM
"If you're trying to move it visually, you can try modifying the CSS to use absolute positioning or some other adjustments.
However, if you're trying to actually pull it out of the DOM and insert it somewhere else, you won't be able to avoid a reload."
~ John Fisher
Also: https://stackoverflow./a/8318401/5444802
"It isn't possible to move an iframe from one place in the dom to another without it reloading."
~ Kevin B
Check the youtube api for iframe embedding https://developers.google./youtube/iframe_api_reference#Loading_a_Video_Player
本文标签: javascriptjQuery appending iframe to div without reloadStack Overflow
版权声明:本文标题:javascript - jQuery appending iframe to div without reload - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744188003a2594384.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论