+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / jQuery $(location).attr('href',URL) not working as desired

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

4 posts, 1 answered
  1. Carlos Redondo
    Carlos Redondo avatar
    21 posts
    Registered:
    09 Jan 2009
    22 Feb 2012
    Link to this post
    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.

  2. Tim
    Tim avatar
    258 posts
    Registered:
    22 Jun 2011
    23 Feb 2012
    Link to this post
    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!

    Answered
  3. Carlos Redondo
    Carlos Redondo avatar
    21 posts
    Registered:
    09 Jan 2009
    28 Feb 2012
    Link to this post
    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.


  4. Carlos Redondo
    Carlos Redondo avatar
    21 posts
    Registered:
    09 Jan 2009
    28 Feb 2012
    Link to this post
    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!



4 posts, 1 answered