jQuery $(location).attr('href',URL) not working as desired - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

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

  • jQuery $(location).attr('href',URL) not working as desired
  • 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,

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

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

    $(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" />

    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!

  • 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.  


  • 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!