onclick page go to homepage without any absolute path - javascript

I want to click on link and I want to go to the page on home page by it self
<a onclick="javascript:GoToHomePage()" href="javascript:void(0)">Home page</a>
function is
function GoToHomePage()
{
window.location = 'default.aspx';
}
but i want the url of page www.testtest.com instead of www.testtest.com/default.aspx
I can't even give the absolute path like www.testtest.com as it's a portal and same page coming in few more portals.

following code will take you to '//www.testtest.com/'
function GoToHomePage()
{
window.location = '/';
}

Try this:
window.location = '/';
Executing this code will send your visitor directly to the root of your website.

It looks like you need
window.location = '/default.aspx';
or
window.location = '/';
(not sure of your goal)
The '/' means "the root of the domain".

Add / before default.aspx and it should take you to home page
function GoToHomePage()
{
window.location = '/default.aspx';
}

I would strongly recommend to use href instead of onclick due to various obvious reasons:
Home page

It's working for me, as suggested by #Olly Hodgson:
Home page

Related

How Can I go to another HTML file

For some reason, document.location.href , window.location.href , and window.location.replace is not working, I'm using localhost for now and I don't know how to redirect it to another webpage. I tried adding http:// on front, it's still not working, I tried making it redirect to another online website, It also didn't work. I read a lot of thread already and tried them but none of them worked for me.
var loginBut = document.getElementById("RDbtn1");
loginBut.addEventListener("click", checklogin);
function check(){
document.location.href = "http://localhost/some/directory/here";
alert(document.location.origin + '/some/directory'); // I just use this to know that the button is being pressed.
}
html:
<!-- some code here-->
<li><button class="login1" id="RDbtn1">LOGIN</button></li>
<!-- some code here -->
I believe you should redirect to particular html page and should give the port on which your local host application is working
document.location.href = "http://localhost:8080/some/directory/here/file.html";
It is window.location (nothing or replace or href) - document.location is normally used to get the actual page
What's the difference between window.location and document.location in JavaScript?
If you are on the same server, no need to qualify the page but I would add the actual page you try to load
window.location = "/some/directory/here/index.html"

Force reload of page with URL containing an anchor (#)

I want to reload a page like so :
window.location.href = url;
But given my url contains an anchor the previous instruction won't do anything.
How to deal with this issue ?
Thank you for the great help.
Just use:
window.location.reload();
You can do location.replace("https:/rickrolled.com");
So, to redirect, you can do location.replace(location.href);
Or just do location.reload();
If it doesn't work, please send me a JSFiddle of your project.

window.location with hash returns to the top of the page [duplicate]

I am trying to refresh the page with this statement in external javascript file after some processes.
window.location.href = window.location.href
It perfectly reload the page but I want to scroll to the specific location after reload.
So, I put this on the page.
<a name="uploadImgSection"></a>
And change the javascript to
window.location.href = window.location.href + "#mypara";
This code doesn't reload/refresh the page either
window.location.hash = "#uploadImgSection";
window.location.href = window.location.href;
When I do that, it doesn't reload the page at all. Is there any way I can reload the page with scroll bar position?
window.location.href += "#mypara";
location.reload();
First add the hash, then reload the page. The hash will remain there, and the page gets reloaded.
Tested, works.
ps: If a hash already exist in the URL, you may want to directly change it via location.hash instead of .href.
I wanted to update this question because I found that using window.location.href += "#mypara" will keep appending the parameter to the url even if it exists. I have found the better way is to use
window.location.hash = "#mypara"
location.reload();
This is really an old post, I would still try and answer with right combination of answers.
location.reload() and location.reload(true) works like F5 on browser. This will post all the form data back to the server if previously load had done it.
location.href will not refresh the page until there is a URL change recognized by the browser. change in hash (#paramname) doesn't qualify for URL change and hence just doing
location.href+="#paramname" will not work.
So, location.href+="?anything#paramname" should reload the page as a new request as ?anything is change in the URL.
var loc = location.hash;
location.hash = " ";
location.hash = loc;
Try this
var urlString=window.location.href;
var url=urlString.split("#")[0];
window.location.href = url + "#mypara";
This worked for me:
var tabValue = document.URL;
window.location = tabValue.substring(0, tabValue.lastIndexOf("#"));
window.location.hash = "#tabs-3"; //Whatever is your hash value
location.reload();
This work for me
window.location.href = baseUrl + "#/m/team";
location.reload();
This worked for me
$('body').on('click', '.className', function () {
var data = $(this).attr('href');
alert(data);
window.location.reload();
});

Redirecting inside a phonegap application is not working

I'm attempting to redirect to a locally stored html file inside of my application but it just refreshes the page... I've tried a few different approach with all the same results. However, document.location.reload works.
Here are a few lines that I've already tried (url being the path to the local html file, I've tried the full url with the file:/// protocol and relative path)
document.location.href = url;
document.location = url;
window.location.href = url;
window.location = url;
window.open(url, "_self");
Thanks
I've ended up with the following solution :
var link = $("<a href='" + url + "'></a>");
$("body").append(link);
link.get(0).click();
I'm not sure these are solutions, but just some things to check for.
Ensure that you're not inadvertently redirecting to yourself (same page).
Ensure that you're not stuck in a loop because you're redirecting to a page that automatically redirects back to the original page.
If you're using jQuery Mobile, try using the page container's Change() method instead.

JS redirect/window replace doesn't work if url is the same (but anchor added)

I want to use JS/jQuery to reload the current page and jump to an anchor.
I've tried using:
window.replace('currentpage/#msg'+.message_id);
And
window.location.href ='currentpage/#msg'+.message_id;
Both change the url in the address but fail to reload the page.
Any other ways to do it?
call this after changing url :
window.location.reload(true)
There are more ways to do it: http://www.phpied.com/files/location-location/location-location.html :)
Try this way
First get the current URL
var CurUrl = window.location;
Now change the redirect URL in the browser without further'
history.pushState(CurUrl, "OldPage", "NewPage");
Now the browser URL has changed but the page was not redirected and get the URL again'
var UrlRedirect = window.location;
here you can write your code
Finish redirecting
window.location = UrlRedirect;
Another way to force the redirect is to make sure that the URL does change, by adding in a random element. I often add a timestamp to the end of a URL.
This has the added benefit or drawback that each page load will look unique to caching systems/etc. You may or may not want this.

Categories