jQuery $(location).attr('href',URL) not working as d

Posted by Community Admin on 04-Aug-2018 17:16

jQuery $(location).attr('href',URL) not working as desired

All Replies

Posted by Community Admin on 22-Feb-2012 00:00

Hi Team,
My requirement seems to be simple, straightforward...  have a submit button within a custom widget consuming a javascript to collect some information from HTML content and redirect web user to an external page... but redirect doesn't work at all.  I tried both javascript and jQuery techniques.

Is there any restriction on Sitefinity blocking redirects to external URL?

Carlos.

Posted by Community Admin on 23-Feb-2012 00:00

Carlos,

Sitefinity shouldn't be blocking any JavaScript, here are some things you can try:

JavaScript:
window.location.href = "http://www.sitefinity.com";

jQuery:
$(location).attr('href', 'http://www.sitefinity.com');

Note: When logged in to the back-end, Sitefinity will load jQuery for you. When you log out, if jQuery isn't loaded on the page, it won't work.

To use the version of jQuery that Sitefinity ships with:
<sf:ResourceLinks ID="resourcesLinks" runat="server">
    <sf:ResourceFile JavaScriptLibrary="JQuery" />
</sf:ResourceLinks>

To reference a specific version that you have in your template:
<script src="/Sitefinity/WebsiteTemplates/Custom/App_Themes/Default/JS/jquery-1.7.1.min.js"></script>

Lastly, and also my favorite! 

To reference a version via CDN and fallback to local if it somehow fails:
<!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="/Sitefinity/WebsiteTemplates/Custom/App_Themes/Default/JS/jquery-1.7.1.min.js"><\/script>')</script>

I learned that last example from the HTML5 Boilerplate.

Hope that helps!

Posted by Community Admin on 28-Feb-2012 00:00

Hi Tim,
Weird but it doesn't work for me.  I tried both JavaScript and jQuery approaches and web browser remains on same page, it doesn't change to desired URL, ie http://www.sitefinity.com.  Notice that I've no JavaScript errors but no web browser navigation at all.  My workaround will be trying open a new window instead.  

Thanks.


Posted by Community Admin on 28-Feb-2012 00:00

Hi Tim, 
I realized my basic-beginner mistake... I missed the "; return false;" command immediately after javascript method call, I was immerse on javascript logic and location change was the last line of code I had to enter to complete this task.. Thanks!



This thread is closed