I'm completely new to using jQuery. Why isn't this working?
<iframe width="800" scrolling="no" src="">your browser needs to be updated.
</iframe>
<script src="jquery.js"></script>
<script> //var c = 0; $(document).ready(function() { $('#prev').contents().html("<html><body><div> blah </div></body></html>"); })
</script>Also, I am planning on using this iFrame to present a preview of changes to an html file to a user. I am going to either empty the entire iFrame every time a change is made or, if I can figure out how to do so, change particular lines using jQuery. Is that the best way to go about this?
13 Answers
If you want to update the iframe content using html then you should first find the body element inside the iframe and then append the required markup.
$(document).ready(function() { $('#prev').contents().find('body').html('<div> blah </div>');
});Working demo -
3I have created a PHP script that can get all the contents from the other website, and most important part is you can easily apply your custom jQuery to that external content. Please refer to the following script that can get all the contents from the other website and then you can apply your cusom jQuery/JS as well. This content can be used anywhere, inside any element or any page.
<div id='myframe'> <?php /* Use below function to display final HTML inside this div */ //Display Frame echo displayFrame(); ?>
</div>
<?php
/* Function to display frame from another domain
*/
function displayFrame()
{ $webUrl = ' //Get HTML from the URL $content = file_get_contents($webUrl); //Add custom JS to returned HTML content $customJS = " <script> /* Here I am writing a sample jQuery to hide the navigation menu You can write your own jQuery for this content */ //Hide Navigation bar jQuery(\".navbar.navbar-default\").hide(); </script>"; //Append Custom JS with HTML $html = $content . $customJS; //Return customized HTML return $html;
} 2 var monitor = setInterval(function(){ var elem = document.activeElement; if(elem && elem.tagName == 'IFRAME'){ var thanks = $(elem).contents().find('.thank-page__title').html(); if (thanks && thanks.indexOf('Спасибо') !== -1) { setYandexTarget('reika-kviz'); clearInterval(monitor); } }
}, 1000); 1