Adding scripts to an iframe using jquery - javascript

I am generating an iframe within a jquery dialog box but am having trouble loading some scripts within it.
function showDialog() {
$("#divID").dialog("open");
$("#modalIframeID").attr("src", "/staff/somepage");
return false;
}
$(document).ready(function() {
$("#divID").dialog({
autoOpen:false,
modal:true,
height:500,
width:960,
closeOnEscape:true,
});
});
I tried adding them through a load event but havent had any success.
function showDialog() {
$("#modalIframeID").load(function()
{
//load scripts here
$("#addFaci").formToWizard();
});
$("#divID").dialog("open");
$("#modalIframeID").attr("src", "/staff/somepage");
return false;
}
Any suggestions would be appreciated.
Thanks,
Tim

If you have access to the file that is being loaded into the iframe you could just add the javascript code into that file and attach it to the document ready event.
If not try reading this following article and see if it will help you out :)
http://huuah.com/jquery-and-iframe-manipulation/

Related

Invoke a javascript function after page loading is finished?

I use CookieBot in our html pages. CookieBot javascript doesn't load in test environment and throws a net::ERR_ABORTED 404 error.
When this happens, the loading spinner in the page keeps displaying after the page loading has been completed.
I tried following options to invoke a listener after page loading is completed. But none of them works:
document.addEventListener("load", (e) => {
console.log("document load");
});
document.addEventListener("DOMContentLoaded", function(event) {
console.log("DOMContentLoaded");
});
$(document).ready(function() {
console.log("ready!");
});
$(document).ready(function() {
console.log("document is ready");
});
$(window).on("load", function(){
console.log("window load!");
});
window.onload = function () {
console.log("window onload!");
};
I guess CookieBot script overrides my listeners. Here is an example where listener is not invoked. When you remove the CookieBot script it runs: https://jsfiddle.net/hkarakose/4by26Lr3/1/
How can I invoke a function after page loading is finished?
You can invoke a function after page loading with:
window.onload = function() {
//here you can write your function and invoke it.
}
Edit:
Maybe I didn't understand your request, but now I'm reading more carefully.
The problem is in the script, right? If you insert the script on HEAD, it will be loaded first.
You could insert the type of the script in this way:
type = "text / plain" with a data-attribute: data-attribute = "script-cookie".
Then change the type once everything has been loaded, like this:
window.onload = function () {
       var allPrefScript = document.querySelectorAll ("script [data-attribute =" script-cookie "]");
allPrefScript [0] .setAttribute ("type", "text / javascript");
}
Although #davilink92's answer works, I solved this issue in a more practical way.
I attached external script loading to window load event:
window.addEventListener('load', function () {
$.getScript("https://consent.cookiebot.com/uc.js?cbid=d180eb7a-8f13-4549-bacc-6d4a6dfb5da8&culture=en");
$("#global-loader").fadeOut("slow");
});
In this way, the script couldn't prevent the window load event listener to be invoked by the browser. And thus, I was able to remove the loading spinner after page is loaded.
I have the habit to use document.addEventListener("load", (e) => {console.log("loaded"); })
Can you show us more code to see if your problem is here? I think all of yours tests are correct and I dont understand why it isnt working.

Loading content to the div using AJAX/jQuery

I am a beginner in playing with jQuery/AJAX, my goal is to load content to the div below:
<div id="blogcontentloaded">
</div>
I came up with .load and it worked, the page loads but it keeps refreshing and loads over and over.
$(function loadBlog(e) {
$('#blogcontentloaded').load('/blog/page1.html');
e.preventDefault();
return false;
});
I tried using e.preventDefault but it doesn't work for me.
Also my goal is to do this without any buttons. When main page loads portion of the page that I want to load along with main page is going to be for updating the content in loaded element.
Thanks for the help!
You can use the javascript load function. It may solve your problem. here you can get some information about windows load and jQuery ready functions.
$( window ).on( "load", function() {
$('#blogcontentloaded').load('/blog/page1.html');
});
You need to wrap the function in the 'ready' function and make sure that it is executed once:
$(document).ready(function(){
$('#blogcontentloaded').load('/blog/page1.html');
});
Have you used the jQuery file on the top of other js files?
Add the jQuery.js file
$(document).ready(function(){
$('#blogcontentloaded').load('/blog/page1.html');
e.preventDefault();
return false;
})

Issues with .mobile.loadPage and external page in Jquery Mobile

In my jquery mobile application I have one page that I want to load external content.
Trying to follow the docs, and my code doesn't produce any script errors, but my external content does not load.
$(document).ready(function () {
$.mobile.ajaxEnabled = false;
//Initialize page container per docs
$("#staff-directory-container").pagecontainer({ defaults: true });
//Get external content into DOM
$.mobile.loadPage("http://another.domain.com/myContent.html", {
pageContainer: $('#staff-directory-container')
});
});
Thanks in advance for any help offered....
Chris
To bind events in jQuery Mobile, use pagecreate which is equivalent to .ready(). To load an external page, use .pagecontainer("load", "target", { options }) as .loadPage() is deprecated and will be removed in jQM 1.5.
In your case, $.mobile.pageContainer is $("#staff-directory-container"). And note that Ajax should be enabled.
$(document).on("pagecreate", "#pageID", function () {
$("#loadBtn").on("click", function () {
/* define new pagecontainer then load */
$.mobile.pageContainer = $("#staff-directory-container").pagecontainer();
$.mobile.pageContainer.pagecontainer("load", "myContent.html");
});
});

Bootstrap Popover Not Working When Loaded With Ajax

When I load Bootstrap popver content with ajax, the popover is not showing.
Javascript:
var id = 1;
$.post("load.php?pageid",
{
pageid:id;
},
function(data,status){
document.getElementById("body").innerHTML=data;
});
HTML response:
hover for popover
<script>
$(function ()
{ $("#example").popover();
});
</script>
When I place the HTML response above directly in the <body id="body"></body> code, the popover works fine. I dont understand what is going wrong here.
The problem is that you're setting up the popover inside of the function $(). Rather than
$(function ()
{ $("#example").popover();
});
It should be just
$("#example").popover();
Or perhaps better
$(document).ajaxComplete(function() {
$("#example").popover();
});
The reason being that any function inside of $() is run when the document is first finished loading, when the "document ready" event is fired. When that code is pasted inside of the original HTML, it works because it's present when the document finishes loading.
When it's the response from an AJAX call, it won't run inside of $(), because the "document ready" event already fired some time ago.
with the help of jQuery you can initialize all things that needs to be initialized using
$(document).ajaxSuccess(function () {
$("[data-toggle=popover]").popover();
$("[data-toggle=tooltip]").tooltip();
// any other code
});
inspired from Olaf Dietsche answer
<script>$(function () { $('[data-toggle="tooltip"]').tooltip()});</script>
Add this at the end of whatever you are loading in with ajax. You should already have this somewhere to opt-in to the tooltip, but put it again to re-initialize the tooltip.

jquery load and javascript

I've seen many topics on this site about using jquery load and loading the script the in the html fragment. I, unfortunately, have not been able to get any of these methods (getscript) to work. Here is the code that loads the html and the supposed javascript.
<script type="text/javascript">
$.ajaxSetup ({
cache: false
});
$(document).ready(function(){
$("#x").click(function() {
event.preventDefault();
$("#content").load("content.html", function(){
$.getScript("imagemod.js")});
return false;
});
});
</script>
When loading fragmented page loads, though, the pages javascript doesn't seem to work. When I load the page not through ajax, the script seems to work. Here is the page that is suppose to have the javascript working but isnt. The page in question is reached by clicking on "large" of the american dogs menu. Help is much appreciated.
You are using an undefined variable 'event' and forgot a semicolon...
<script type="text/javascript">
$.ajaxSetup ({
cache: false
});
$(document).ready(function () {
$("#x").click(function(event) { // event object is passed as first arg
event.preventDefault();
$("#content").load("content.html", function () {
$.getScript("imagemod.js");
}); // semicolon
return false;
});
});
</script>
Also be aware, that if you load an html document like
<html>...</html>
and not a fragment like
<div>...</div>
browser's won't properly import that into an existing document, because it's a standalone DOM document. To do ajax and partial html, load a fragment like <div>...</div>.

Categories