setTimeout for delaying opening a popup in Javascript - javascript

I need for delaying opening of a popup.
This is the code of my popup:
<script type="text/javascript">
var pmauid = 'Numbers';
var pmawid = 'Numbers';
var fq = '0';
</script>
<script type="text/javascript" src="http://mypopnetwork.com"></script>
So, I need for apply a delay in this part of code:
<script type="text/javascript" src="http://mypopnetwork.com"></script>
If I editing in this way, nothing happened:
<script type="text/javascript" window.setTimeout() src="http://mypopnetwork.com"></script>
Maybe I wrong syntax. This popup is normally open after clicking on an alertbox.
I tried also in this way:
setTimeout(function () {
<script type="text/javascript" src="http://mypopnetwork.com"></script>;
}, 5000);

For one, you are treating JavaScript as if it were some sort of attribute, It should be in between the <script> tags (if your doing it inline) like:
<script>
// your code here
</script>
or if you want to reference a JavaScript file:
<script src="./whatever.js"></script>
or
<script src="http://foo.com/script.js"></script>
Second off, you need to provide a callback and a timeout length to setTimeout(). So in the end, you code should look like:
setTimeout(function () {
alert("whatever");
}, 5000);
Where the callback is what you want to fire after the timer and 500 is the delay in milliseconds.

Related

how can i attach an event to a class

I had link and it automatically has a binding with jquery modal window which when i click, it opens the popup, i want to attach another event to it but it is just ignoring my code
i have this in my code just to see if i can execute the command or not, once it does, i want to call a coldfusion function to run
<script type="text/javascript" defer async>
$(document).on('click',".info",function() {
alert("hello");
});
</script>
if i get an hello,
then i am going to do this
<script type="text/javascript" defer async>
$(document).on('click',".info",function() {
<cfoutput>#runmyfunction()#</cfoutput>
});
</script>
Instead of calling another onClick, why not call the function within the existing onClick?
<script type="text/javascript" defer async>
$(document).on('click',".info",function() {
alert("hello");
otherClickFunction();
});
function otherClickFunction() {
<cfoutput>#runmyfunction()#</cfoutput>
}
</script>

Getting an AJAX call to reload a div every x minutes

I'm new to Javascript and AJAX so please excuse my newb question. I have an AJAX call (that is working) and I'd like to have it refresh the contents of a div every 5 minutes (I'm not using jquery). This is how I'm calling the AJAX function in the <head></head> of my html page:
<script type=text/javascript>
setInterval(ajaxCall(), 300000);
</script>
The initial page load populates the div, but the content of the div doesn't refresh after 5 minutes. What am I doing wrong?
Change your code to this:
setInterval(ajaxCall, 300000);
To pass an argument:
setInterval(function(){ ajaxCall(someCoolValue); }, 300000);
Notice the lack of parentheses in ajaxCall. You want to pass the function itself, not call the function. More examples on MDN.
You need to wrap your ajaxCall.
<script type=text/javascript>
setInterval(function(){ ajaxCall(); }, 300000);
</script>
Here
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
</head>
<div id="links">
</div>
<script language="javascript" type="text/javascript">
var timeout = setTimeout(reloadChat, 300000);
function reloadChat () {
$('#links').load('test.php #links',function () {
$(this).unwrap();
timeout = setTimeout(reloadChat, 300000);
});
}
</script>
Also you are calling it wrong correct syntax would be ,
setInterval(ajaxCall, 300000);
Hope it helps :D

javascript code not work in HEAD tag

My webpage has the following code:
<html>
<head>
<title>This is test Page</title>
<script language="javascript" type="text/javascript">
document.getElementById("msg1").innerHTML = document.URL.toString();
</script>
</head>
<body>
<div class="sss">
<p id="msg1"></p>
</div>
</body>
</html>
As you now at the time the script executes the div doesn't exist but I want to put my JavaScript code only In the <head> tag and I won't put it in middle of HTML code.
But this code only works when I put the <script> tag after the <div> tag.
I use VS2010 and firefox 19.0.1
Is there anyway to put code in <head> tag?
Your script relies on the DOM being ready, so you need to execute that function call only after the DOM is ready.
<script language="javascript" type="text/javascript">
window.onload = function() {
document.getElementById("msg1").innerHTML = document.URL.toString();
}
</script>
The various tags in your HTML page are loaded and processed in the order in which they appear on the page. Your <script> tag is executed immediately when it is parsed in the <head>. This is before the <body> and the elements inside the <body> are parsed. So, the script tries to reference an element that is not defined at the time it is executed.
Michael Geary is right, in order to execute your code, I'd use jQuery library (a de-facto standard in JS development) and utilize the DOM ready event. This will ensure the code in the handler will execute once DOM is fully loaded.
<script>
$(function(){
$('#msg1').html(document.URL.toString());
});
</script>
I recommend to to use addEventListener like this:
<script language="javascript" type="text/javascript">
document.addEventListener("DOMContentLoaded",() => {
document.getElementById("msg1").innerHTML = document.URL.toString();
});
</script>
Your script uses dom element and must run after the dom loaded.
Wrap your code in a function and call it after dom loaded
function myfunc(){
//code here
}
window.onload = myfunc();

Dissect my jQuery - can't use keyup function to insert number of characters into another textarea

I'm trying to figure out why my jQuery isn't working. I've got jQuery linked to, and then after that, I try to bind to the textarea content. I've used name=content and name=#content both.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$('input[name=content]').bind('keyup',function(){
$('#desctag').val($(this).length)
});
</script>
Why isn't it working?
Code is now:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('input[name=#content]').bind('keyup',function(){
$('#desctag').val($(this).length)
})})
</script>
You need to put it in a document.ready function so it executes when the DOM is ready.
$(document).ready(function() {
$('input[name=content]').bind('keyup',function(){
$('#desctag').val($(this).length);
});
});

How to run JavaScript code before page load?

I have working in asp.net web application. Here I need to run JavaScript before page load.
I have tried:
<body oninit="funinit();" onprerender="funRender();" onload="funload();">
</body>
<script type="text/javascript" language="javascript">
function funinit() {
alert("funinit");
}
function funload() {
alert("funload");
}
function funRender() {
alert("funRender");
}
</script>
here only funload() is working.
You can use window.onpaint for such purpose like :
<script type="text/javascript">
function preloadFunc()
{
alert("PreLoad");
}
window.onpaint = preloadFunc();
</script>
I hope it helps you....
Just inline it?
<script type='text/javascript'>
alert("funload");
</script>
Or put it in a function and call it immediately. Try to put it to the topmost of your page, however since the DOM isnt loaded yet you cant get any other elements.
What is it you want to do?
just insert a <script> tag wherever inside the body you want it to run. it will be executed as soon as the parser reads it, as long as it doesn't reference an element not yet created
try to put your script in head section of the page:
<head>
<script type="text/javascript" language="javascript">
alert("funinit");
alert("funRender");
</script>
</head>
Why not Use the ClientScriptManager.RegisterClientScriptBlock Method
http://msdn.microsoft.com/en-us/library/btf44dc9.aspx

Categories