why refresh html page is slow - javascript

For some reason the method display is being processed before location.reload(). Here is my syntax:
$(document).ready(function() {
$("#refresh").click(function() {
alert("before refresh")
location.reload();
display()
})
})
function display() {
alert("hello world")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="refresh">
refersh and continue
</button>
My goal here is to refresh the page when the button is clicked then call display method.
The problem is that display method is being called before the page is refreshed.
First of all, is what I am trying to achieve achievable. If not, what other ways can go take in order to solve the problem?

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
display();
$("#refresh").click(function(){
alert("before refresh")
//location.reload();
display();
});
})
function display(){
alert("hello world")
}
</script>
<button id="refresh">
refersh and continue
</button>
</body>
</html>
Try this, call the display method on loading the document and remove the reload(), if needed add display() in refresh.

You could try assigning your display method to the document.onload event. Try changing your click handler to:
alert('before refresh');
$(document).on('load', display);
location.reload;

Related

auto click a link when page loads doesn't work

I have a page for redirect purpose, I want to redirect user to another page once it hits this page:
<!DOCTYPE html>
<html>
<head>
<title>Open App</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
alert("test"); //this shows up.
$('#openApp').click(); //this button is not clicked.
});
</script>
</head>
<body>
redirect
</body>
</html>
I want the redirect happen once this page is load. But the click function is not fired. I have to click the link by myself to redirect to google.com.
I normally wouldn't give you an alternative as an answer, but looking at your comments, it looks like you're just trying to redirect.
You can use window.location.replace('http://www.google.com'); for that.
$(document).ready(function () {
window.location.replace('http://www.google.com');
});
See also, this answer.
It's not possible to trigger a click event without any user interaction. In other words, if the function being called wasn't called from a user interaction, the click event will not get triggered.
In your case, simply update the window.location attribute:
window.location = 'http://www.google.com';
try this fiddle mate, added script is
$(document).ready(function () {
//this shows up.
$('#openApp')[0].click(); //this button is not clicked.
});

how to click an anchor tag from javascript or jquery

Have an anchor tag, trying to click it from the javascript but not responding, while loading the page it should go to next.php without click anchor tag manually.how can I archive this?
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="jquery-2.0.3.js"></script>
<script type="text/javascript">
alert("hai");
$(document).ready(function() { $('#about').click(); });
</script>
</head>
<body>
<div>
click here
</div>
</body>
</html>
Use $('selector')[0], as $('selector') returns a jQuery object, so $('selector').click() will fire the click handler, while $('selector')[0].click() would fire the actual click.
$(document).ready(function () {
$('#about')[0].click(); //$('#about').get(0).click();
});
Demo
You can not use javascript to fire click event
It should use
<script type="text/javascript">
$(document).ready(function() {
document.location.href='/next.php'
});
</script>
Here is the code that can help you
$(document).ready(function() {
$(document).ready(function() {
$('a').trigger('click');
});
})
function abc() {
alert("");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
click me not
If you want the recommended way then use this inside $(document).ready
window.location="where ever you want to go";
If you want the page to go to next.php without clicking then place the window.location directly into $(document).ready(function() { });
Click() function is used when you want to trigger click event. ie. when you want to trigger some action when anchor tag is clicked.
<script>
alert("hai");
$(document).ready(function() {
window.location = 'next.php'; //If you wish the page to automatically go to next page on page load.
$('#about').click( // If you wish to do something clicking anchor
function(){
alert("Hello");
});
});
</script>
$(document).ready(function() { $('#about').trigger('click'); });
Updated:
$(document).ready(function() { $('#about')[0].click(); });

Jquery on click function called when the page is loaded

I'm trying to add a function to a button but the function is called when the page is loaded when the button is clicked, getMemberByName is on another js file and works correctly
<input type="button" value="miembro" id="boton2" />
<script>
$("#boton2").click(getMemberByName('nombre1','apellido1'));
</script>
here is the proper way to bind such event with jQuery:
<script>
$(function(){
$('#boton2').click(function(){
getMEmberByName('nombre1', 'apellido1');
});
});
</script>
hope that helps.
You need to wrap them in functions:
<script>
$(document).ready(function(){
$("#boton2").click(function(){
getMemberByName('nombre1','apellido1');
});
});
</script>
Docs: http://api.jquery.com/click/
Try this instead:
$("#boton2").click(function() {
getMemberByName('nombre1','apellido1');
});
If this can help someone, as #Moosman says, you need to wrap what you want to do in a function and you must not call. Example:
<script>
$("#boton2").on("click", function(){getMemberByName('nombre1','apellido1')});
</script>
OR:
<script>
$("#boton2").on("click", myfunction);
function myfunction() {
getMemberByName('nombre1','apellido1')
}
</script>
Watch out to not call the function $("#boton2").on("click", myfunction()); because this will call the function just when you load the page and not after, for my experience.

Click link after focus is returned from print dialog

I have a web page that brings up the print dialog on load. After clicking print (and focus returning to the page) I need a link to be clicked. Does anyone know a way using jQuery or Javascript that I can do this?
This is what I currently have, but it doesn't seem to be working:
<script type="text/javascript">
$(document).ready(function() {
window.print();
$("body").bind("focus", function(){ $("#logout").click() });
});
</script>
try this:
<script type="text/javascript">
$(document).ready(function() {
if (window.print())
location.href = $("#logout").attr('href');
else
location.href = $("#logout").attr('href');
});
</script>
and
<body>
<a id="logout" href="http://www.google.it">Link</a>
</body>

hide div when page load

I have jquery issue, Kindly see my jquery code:
$(document).ready(function(){
$(".toggle_container").show();
$("h2.trigger").toggle(function(){
$(this).addClass("active");
}, function () {
$(this).removeClass("active");
});
$("h2.trigger").click(function(){
$(this).next(".toggle_container").slideToggle("slow,");
});
});
My .toggle_container is shown always its good, But I want to close it first time, when page load. Can you please provide me any solution?
I dont want to use $(".toggle_container").hide(); function for this problem because when i click on href then .toggle_container should not hide, It should open.
Regards.
You can just add attribute
style="display:none"
to your element .toggle_container.
Then on first call of $(document).ready it will be shown.
The full test example:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("h2.trigger").click(function(){
$(this).next(".toggle_container").slideToggle("slow,");
});
});
</script>
</head>
<body>
<h2 class="trigger">Toggle</h2>
<div class="toggle_container" style="display:none">Container</div>
</body>
</html>
Note: there`s no $(".toggle_container").show(); on $(document).ready
remove the
$(".toggle_container").show();
from your $(document).ready function.
in html part add style="display:none" for the toggle_container div.
check the #HCK s reply. he is clearly mentioned it..
Once the document gets loaded, a alert box will be prompted "page loaded".
$(document).ready(function(){
alert('page loaded'); // alert to confirm the page is loaded
$('.divClassName').hide(); //enter the class or id of the particular html element which you wish to hide.
});

Categories