0

I have include all the link to my javascript file in my master page using tag. Can I load specific js file according to pages loaded? (for example, I would like to load home.js only when Home.aspx is loaded).

  • 1
    Can't you just put the reference to home.js in the header of the home.aspx? So it will be loaded only when you navigate to Home.aspx. Isn't this what you want? – Norbert Aug 19 '13 at 8:13
  • As above (Norbert). Or use dynamic names of your files that match for instance home.aspx loads home.js with a document.url parse. – Paul Leigh Aug 20 '13 at 15:29
0

create an event handler that runs on page load! this can be added to a feature to turn off and on!

within the event handler you can add:

    private string BindScript(string ScriptUrl, bool PickFromSiteCollection)
    {
        if (PickFromSiteCollection)
            ScriptUrl = Microsoft.SharePoint.Utilities.SPUrlUtility.CombineUrl(SPContext.Current.Site.RootWeb.Url, ScriptUrl);
        else
            ScriptUrl = Microsoft.SharePoint.Utilities.SPUrlUtility.CombineUrl(SPContext.Current.Web.Url, ScriptUrl);
        return string.Format(@"<script type=""text/javascript"" src=""{0}""></script>", ScriptUrl);
    }

you can call the above method on the page load event! do it like so:

writer.Write(BindScript("/_layouts/1033/js/jquery.min.js", true));

so when you first add the wsp you can activate the feature, the feature will run on each page load event. obviously you need to have a case statment or if function to check what page its on! if its on a specific page you can then bind the JS to the masterpage!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.