Jquery to load java script content into a div - javascript

I have the following code:
var refreshId = setInterval(function()
{
$("#footad").html('myjshere');
}, 15500);
Where it says myjshere I want to load this content into the div:
<script type='text/javascript'>
AdServer.placeAd({"sz":"728x90","pos":1});
</script>
But when I try just having it where myjshere is, it throws out a syntax error?
Any help?
To clear up the confusion I was put the JavaScript where myjshere is I just used the word myjshere as a place holder to show you what I was doing. Sorry for the confusion. The issue is that when I put the javascript within the jQuery it does not work and returns an error: invalid syntax.

What your script is doing is putting 'myjshere' into the element with id 'footad' .
Is that what you are trying to achieve?
If I understood right, what you want, then this is the code:
var refreshId = setInterval(function()
{
$("#myjshere").html("<script type='text/javascript'> AdServer.placeAd({'sz':'728x90','pos':1});</script>");
}, 15500);
Else if you want to replace myshere
var refreshId = setInterval(function()
{
$("#footad").replace("myjshere","<script type='text/javascript'> AdServer.placeAd({'sz':'728x90','pos':1});</script>");
}, 15500);

I'm assuming AdServer.placeAd is a third-party function you want to implement. Therefore it depends on what the output of this function is. If Adserver.placeAd returns a HTML string you could do the following:
<script type='text/javascript'>
var refreshId = setInterval(function()
{
$("#footad").html(AdServer.placeAd({"sz":"728x90","pos":1}));
}, 15500);
</script>
My guess is that this code is running document.write() inside the function seeing as its called placeAd. Therefore you could add the code into the part of the HTML document you want it to appear like:
<script type='text/javascript'>
setInterval(function()
{
AdServer.placeAd({"sz":"728x90","pos":1});
}, 15500);
</script>

i better dont aks why you want to do this....
$('#footad').html('<script type="text/javascript">
AdServer.placeAd({"sz":"728x90","pos":1});
</script>');

do you want to see the js as plain text you can load it with:
var js = AdServer.placeAd({"sz":"728x90","pos":1});
$("footad").html(js);
but will the js function placeAd return html? if not you must give an target to the AdServe class. like Adserver.target("#footad");
if this all is not that what you need, you must give more informations.

You need to do it like this
<script type='text/javascript'>
var myjshere = AdServer.placeAd({"sz":"728x90","pos":1});
$(document).ready(function(){
setInterval(function() {
$("#footad").html(myjshere);
}, 15500);
});
</script>

Related

Why doesn't my page auto refresh properly?

My goal is to refresh the page when the class p.fancybox-error is visible, but I would like to know why this part of the code doesn't work at the top of my page.
<script type="text/JavaScript">
var theDiv = document.querySelector("p.fancybox-error");
theDiv.addEventListener("click", function() {
setTimeout(function(){ location.reload(); }, 5000);
});
</script>
I put it under this line:
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
But it gives me this error:
null is not an object (evaluating 'theDiv.addEventListener')
Thanks a lot guys, just take a look to #Stephane's answer, it works perfectly for me.
This polls every second looking for your class and auto-reloads when it exists
$(function() {
setInterval(function() {
if($("p.fancybox-error").length) location.reload();
}, 1000);
});

jquery - ajaxStop is not consistently executing javascript redirect

I am using jquery 1.9.1. I have logout code where I need to post to multiple pages then do a redirect:
<script type="text/javascript">
window.onload=function(){
$( document ).ajaxStop(function() {
logoutRedirect();
});
logoutOfApps();
}
</script>
These methods are in a different javascript file (but I think that is irrelavent).
function logoutOfApps(){
$.post("/app1/logout");
$.post("/app2/logout");
}
function logoutRedirect(){
var redirectOnLogoutURL = null;
redirectOnLogoutURL = "/mylogoutdisplaypage";
top.location = redirectOnLogoutURL;
}
SECOND TRY: This also doesn't work consistently:
<script type="text/javascript">
window.onload=function(){
logoutOfApps();
logoutRedirect();
}
</script>
Make sure both requests have finished before you redirect
function logoutOfApps(){
return $.when(
$.post("/app1/logout"),
$.post("/app2/logout")
)
}
logoutOfApps().done(logoutRedirect);
Just for completeness thought I would document exactly how I changed it, which took a little while to get just right. Thank you adeneo for your answer that I accepted.
<script type="text/javascript">
window.onload=function(){
onLoadOfPage();
}
</script>
This is in the second file (I preferred all jquery code in one file):
function onLoadOfPage(){
$( document ).ajaxStop(function() {
logoutRedirect();
});
logoutOfApps().done(logoutRedirect);
}
function logoutOfApps(){
return $.when(
$.post("/app1/logout"),
$.post("/app2/logout")
)
}
function logoutRedirect(){
var redirectOnLogoutURL = null;
redirectOnLogoutURL = "/mylogoutdisplaypage";
top.location = redirectOnLogoutURL;
}

Quit function on body onLoad

I need a script thats redirects the user to a second side, when the mainpage need to long to load.
I did this:
<script type='text/javascript'>
$(document).ready(function() {
setTimeout("location.replace('contact.html')",10000);
});
</script>
To sametime a preloader opens an get hided on on body onload, when that happens i need to kill the functon above?
Anybody an idea?
Try this:
var redirect = setTimeout(function(){
window.location = 'somewhere'
}, 10000)
$(document).ready(function() { // or $(window).load(function() {
clearTimeout(redirect);
});
setTimeout("location.replace('contact.html')",10000);
Instead, you can try
setTimeout(function(){window.location='contact.html'},10000);
You Can try Following
var Ispreloader=false;
<script type='text/javascript'>
$(document).ready(function() {
if(!Ispreloader)
{
myfunction();
}
});
function myfunction()
{
setTimeout("location.replace('contact.html')",10000);
}
//------------Whenever preploadeer loads on bodu unload set the variable to true
Ispreloader=true;
</script>

jquery - call function in external js file

If I have this code in a file called custom.js:
var kennel = function(){this._init();};
kennel.prototype = {
_init: function() {
this.setListeners();
},
setListeners: function(){
...
},
getCats: function(){
alert("Get cats");
}
};
How do I call getCats() from some arbitrary html file?
First of all you would include the javascript by using the <script> tag, generally in the <head>, then after that you can create another <script> tag which you can place your own javascript code in that is specific to the page, and which uses the functionality made available by the included file. For example:
<head>
<script type="text/javascript" src="path/to/custom.js"></script>
<script type="text/javascript">
var myKennel = new kennel();
myKennel.getCats(); // alerts "Get cats"
</script>
</head>
Also, the code you have posted has nothing to do with jQuery - it is plain old, vanilla javascript.
This should be enough:
<script>
var k = new kennel();
k.getCats();
</script>
Here's a sample of it working: http://jsfiddle.net/NHaBb/
​

Js javascript code problem

Hi i have a problem with my js code. So, i include in 'head' in my site, script
var Engine;
jQuery
(
function($)
{
Engine =
{
utils :
{
site : function(id)
{
$('#content').html('<strong>Please wait..</strong>').load('engine.php',{'site':id},function()
{
os = {adres:'drugi'};
history.pushState(os,'s',ts);
}
);
},
topnav : function()
{
$('div li').bind('click',function()
{
Engine.utils.site($(this).attr('rel'));
unbind('click',false);
}
);
}
}
};
Engine.utils.topnav();
}
);
Now i want call included script in index.php
<script language="JavaScript" type="text/javascript">
Engine.utils.site(1);
</script>
And here is a problem above code doesn't works.
But if i click on any 'li' element Engine.utils.site work's correctly.
Please help me i try fix it a few days.
Sory for my bad english
Have you tried putting the call in a ready function?
<script language="JavaScript" type="text/javascript">
$(function() {
Engine.utils.site(1);
});
</script>
This happens because of closure. Your object Engine is not visible outside anonymous function

Categories