Please help me on this how can i use hash function to go to another tab or id without location.reload();.
it should be needed to go to the download tab or id without needing to refresh the page.
here is my code
<script>
function Download() {
window.location.hash = "#Download";
location.reload();
}
</script>
Do you actually need to use JavaScript for this? What about just using #href attributes to go to another <a> link on the page?
Something like this:
<a id='first' href='#second'>Go to Second Link.</a>
<a id='second' href='#first'>Return to First Link.</a>
See this fiddle for example: https://jsfiddle.net/3axqpr1x/1/
you can try this one;
<script>
function Download() {
window.location.hash = "#Download";
location.reload();
}
</script>
Please refer this page Javascript reload the page with hash value
JavaScrpt expert,
i want if the below script exist in my template coding then my page should redirect to example.com
<script>
$(document).ready(function(){
$("#wrapper").hide();
})
</script>
if the above script exist in my template, then it should redirect to example.com
Attention: please add some condition in that script like this:
<script>
$(document).ready(function(){
If
//#wrapper is hide
$("#wrapper").hide();
//then it should redirected to example.com
</script>
I hope someone will figure out and will share code with me. thanks.
If you need this functionality somewhere after the bit of code you show, this would work:
var $wrapper=$("#wrapper");
if($wrapper.length>0 && !$wrapper.is(':visible')){
// #wrapper exists on the page but is not visible, redirect user
window.location.href = "http://example.com";
}
What Taplar says is:
<script>
$(document).ready(function(){
// $("#wrapper").hide();
window.location.href = "http://example.com";
})
</script>
If you need this behaviour in another place in your code, then see DelightedD0D answer.
Very good point by DelightedD0D, I've fixed the code. ;)
DelightedD0D, I'd give you another point if I could.
I have a link and I need it to do two thing when it's clicked. First, it tracks the h it using the google tracking code, and the next is that it opens a new window.
I tried:
onclick="javascript: pageTracker._trackPageview('/Ads/MMA_Front_Page.com'); window.open(this.href, '_blank'); return false;"
But it didn't work, it didn't open a new window.
Did I do something wrong with the code or do I need to create a javascript method and call that?
Thanks
Edit: I am trying with functions, but it wont work. Can you help me please?
Here is the function:
<script type="text/javascript">
function openAd(adType) {
pageTracker._trackPageview(adType);
window.open(this.href, '_blank');
return false;
}
</script>
And the part where i call it:
onclick="openAd('/Ads/MMA_Front_Page.com')"
It seems only the first part is working "pageTracker._trackPageview(adType);" and the rest is being ignored.
You should really use functions inside your <script> tag but, try without the javascript:... like:
onclick="pageTracker._trackPageview('/Ads/MMA_Front_Page.com'); window.open(this.href, '_blank'); return false;"
onclick="do_something();"
<script type="text/javascript">
function do_something(){
pageTracker._trackPageview('/Ads/MMA_Front_Page.com');
window.open(this.href, '_blank');
return false;
}
</script>
You have included answer in your question. It will be better to create a function for multiple statement.
How can I reload an HTML base web page only once? I am using history.go(0); with function onLoad in body tag but i want to run it only once. Please keep in mind that I am using iframe so this is not possible to to use the below types code:
<script language=" JavaScript" ><!--
function MyReload()
{
window.location.reload();
}
//--></script>
<Body onLoad=" MyReload()" >
The above code did not work for me.
However the code below is working well but the problem is that I need it to load only once:
<BODY BGCOLOR=#FFFFFF background="../images/Index_04.jpg" onLoad="history.go(0)" >
Note: I am using two iframes when user click on main page link a page loads in iframe then I want to reload the whole page with current iframe page.
You could use a querystring at the end of the page url (e.g. ?r), and check for it before redirecting, as if it exists, the redirect is already done.
if(window.location.href.substr(-2) !== "?r") {
window.location = window.location.href + "?r";
}
Disclaimer: I agree with others that you probably have a better solution - and should never need to refresh 'just once'.
Explanation of use
At the bottom of your page, just above the </body> you should put this:-
<script type="text/javascript">
if(window.location.href.substr(-2) !== "?r") {
window.location = window.location.href + "?r";
}
</script>
That's it.
<script type="text/javascript">
$(document).ready(function(){
//Check if the current URL contains '#'
if(document.URL.indexOf("#")==-1)
{
// Set the URL to whatever it was plus "#".
url = document.URL+"#";
location = "#";
//Reload the page
location.reload(true);
}
});
</script>
Due to the if condition the page will reload only once. Hope this will help.
Here is another solution which works for me using localStorage. In this solution we don't need to modify existing url.
<script type='text/javascript'>
(function()
{
if( window.localStorage ){
if(!localStorage.getItem('firstReLoad')){
localStorage['firstReLoad'] = true;
window.location.reload();
} else {
localStorage.removeItem('firstReLoad');
}
}
})();
</script>
The only way I can think of to do this is by using cookies (or if your on the right platform, sessions) and flag the first time it has reloaded, variables might also work in this case, because we're speaking about IFRAME, but I have never tried such thing.
We have a JavaScript function named "move" which does just "windows.location.href = any given anchor".
This function works on IE, Opera and Safari, but somehow is ignored in Firefox. Researching on Google doesn't produce a satisfactory answer why it doesn't work.
Does any JavaScript guru knows about this behavior, and what would be the best practice to jump to an anchor via JavaScript?
Have you tried just using
window.location = 'url';
In some browsers, window.location.href is a read-only property and is not the best way to set the location (even though technically it should allow you to). If you use the location property on its own, that should redirect for you in all browsers.
Mozilla's documentation has a pretty detailed explanation of how to use the window.location object.
https://developer.mozilla.org/en/DOM/window.location
If you are trying to call this javascript code after an event that is followed by a callback then you must add another line to your function:
function JSNavSomewhere()
{
window.location.href = myUrl;
return false;
}
in your markup for the page, the control that calls this function on click must return this function's value
<asp:button ........ onclick="return JSNavSomewhere();" />
The false return value will cancel the callback and the redirection will now work. Why this works in IE? Well I guess they were thinking differently on the issue when they prioritized the redirection over the callback.
Hope this helps!
One observation to ensure in such a scenario
Following will work in IE, but neither in Chrome nor in Firefox (the versions I tested)
window.location.href("http://stackoverflow.com");
Following will work all the three
window.location.href = "http://stackoverflow.com";
Maybe it's just a typo in your post and not in your code, but it's window and not windows
I am not sure to follow you.
I just tried: going with FF3 to Lua 5.1 Reference Manual (long and with lot of anchors).
Pasting javascript:window.location.href="#2.5"; alert(window.location.href); in the address bar, I went to the right anchor and it displayed the right URL. Works also with a full URL, of course.
Alternative code: javascript:(function () { window.location.href="#2.5"; })();
Perhaps you forgot the #. Common problem, also with image maps.
I have the same problem and I guess this is related to a click event.
I have a function that moves the browser to a specific page. I attach that function to some click events: in a button and in a image. AlsoI execute the function when the user press escape (document onkeypress event).
The results are that in all cases the function is called and executed, but only when there is a click the browser goes to the address I want.
Update
I got it working! with a
setTimeout( "location.replace('whatever.html');", 0 );
I don't know why the location.replace wasn't working when the event was a keypress, but with the settimeout it works :)
Update
Returning false after the event when you press escape makes the redirection works. If you return true or nothing the browser will not follow
You've got to add return false; after the window.location.href as mentioned above.
function thisWorks()
{
window.location.href = "http://www.google.com";
return false;
}
function thisDoesNotWork()
{
window.location.href = "http://www.google.com";
}
window.location.href works fine in all versions of Firefox, as does document.location.href I think that there is something else in your code that is breaking things.
drop this in a blank page, if it works, it indicates there is something else wrong on your page.
<script>
window.location.href = 'http://www.google.com/';
</script>
You could also use window.location.replace to jump to an anchor without register it in the browser history:
This article illustrates how to jump to an anchor and uses href as read-only property.
function navigateNext()
{
if (!window.location.hash)
{
window.location.replace(window.location.href + unescape("#2"))
}
else
{
newItem = nextItem(window.location.hash)
if (document.getElementById(newItem))
{
window.location.replace(stripHash(window.location) + "#" + newItem)
}
else
{
window.location.replace(stripHash(window.location) + "#1")
}
}
}
Have you tried this?
Response.Write("<script type='text/javaScript'> window.location = '#myAnchor'; </script>";);
please add full javascript script tag
<script type="text/javascript" language="javascript"></script>
window.location.hash = "#gallery";
For reference I had the same problem.
onclick = "javascript: window.location('example.html');" didn't work under FF (latest)
I just had to rewrite to onclick = "javascript: window.location = 'example.html';" to get it working
I just overcome the same problem. and the problem is not in javascript, but the href attribute on the <a> element.
my js code
function sebelum_hapus()
{
var setuju = confirm ("Anda akan menghapus data...")
if (setuju)
window.location = "index.php";
}
my previous html was
Klik here
and I update it to
Klik here
or remove the href attribute
hope this helps.
window.location.assign("link to next page") should work in both (chrome and firefox) browsers.
window.location.assign("link to next page")
Another option:
document.location.href ="..."