Tuesday, 18 November 2014

Javascripts Error - Location.href="#" does not open the site and it append with the existing URL in SharePoint 2010 webpart

Problem:

I have faced a problem with a javascript code so i want to share this to all.

Working on a page in Sharepoint 2010 ( page URL imagine http://abc.com/sites/pages/abc) where i need to redirect the page on click of a button.


Button calls a javascript function called redirect().

<script>
redirect()
{
location.href="www.google.com";
}

</script>

but on click the page redirected to a URL which appended the www.google.com and the current URL.

so when i see in browser the URL= http://abc.com/sites/pages/abc/www.google.com

obviously this will throw an error message on the browser either page not found or administrator has not allowed you to view the page.

i was breaking my head why it is happening then i have changed the redirect method like below:

<script>
redirect()
{
location.href="http://www.google.com";
}

</script>

This time i have added the http:// to google.com and after button click it redirects to google.com and worked perfectly.

so the thing is we need to provide the protocol as well when we are using the location.href=URL in a javascript function.

Hope it will help somebody since i spend this on 3hr to figure out the issue. google was not saying much about this may be its very basic concept everybody should know but i did not know before.

Please feel free to provide any feedback

No comments:

Post a comment