none
How to insert javascript into a page layout or master page RRS feed

  • Question

  • I posted to another forum asking how strip everything from a URL after the aspx so I can pass it to an href link. The script is (and my question continues afterwards):

    <script type="text/javascript">
    function strip()
    {
    if(window.location.search)
    {
    var l=window.location.href;
    window.location=l.substring(0,l.indexOf('?',0));
    }
    }
    </script>

    and then the html tag is: <a href="#" onclick="strip();">[Save changes]</a>

    However, when I add this to the page layout using designer, I get a SP Foundation Error. It's the script that's the problem, not the html tag. So, where do I insert the script?

    Thursday, October 6, 2011 8:31 PM

Answers

  • After inserting that, it gave me an error in Designer, but also the option to re-assign the correct objects from the Master Page (I'm probably mis-remembering the specific wording in the message that popped up). But anyway, I did what Designer recommended and it now works. To be honest, I'm not sure what I just did to make it work, but it does ;-) I hope it doesn't cause some other problm down the road.

    Thanks for the help.

    Friday, October 7, 2011 1:12 AM

All replies

  • What is the error you get.  You can insert the script tag anywhere you can put html and while it goes against standards, it is allowed.  The correct page to put the script is in the PlaceHolderAdditionalPageHead placeholder which you can do in the master page or page layout.
    -James
    MCPD: SharePoint Developer 2010
    Posting is provided "AS IS" with no warranties, and confers no rights.
    Please mark as answer or helpful as appropriate
    Thursday, October 6, 2011 9:30 PM
  • Like this?

    <asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">

    <script type="text/javascript">
    function strip()
    {
    if(window.location.search)
    {
    var l=window.location.href;
    window.location=l.substring(0,l.indexOf('?',0));
    }
    }
    </script>


    </asp:Content>

    Friday, October 7, 2011 12:38 AM
  • Yes, that should be it (depending on the page layout).
    -James
    MCPD: SharePoint Developer 2010
    Posting is provided "AS IS" with no warranties, and confers no rights.
    Please mark as answer or helpful as appropriate
    Friday, October 7, 2011 12:58 AM
  • After inserting that, it gave me an error in Designer, but also the option to re-assign the correct objects from the Master Page (I'm probably mis-remembering the specific wording in the message that popped up). But anyway, I did what Designer recommended and it now works. To be honest, I'm not sure what I just did to make it work, but it does ;-) I hope it doesn't cause some other problm down the road.

    Thanks for the help.

    Friday, October 7, 2011 1:12 AM