0

I have a couple of small javascript functions:

// Show cookie notice
$('#site-cookie-notice').slideDown();

// Hide cookie notice
$('.close-cookie-notice').click(function (e) {

    if (e.preventDefault) {
        e.preventDefault();
    }
    else {
        e.returnValue = false;
    }

    $('#site-cookie-notice').slideUp();
});

The functions are only relevant to a single user control, that appears a maximum of once in a user visit (not at all for returning customers). The rest of the time the control is not rendered.

The above code currently resides in my global.js, inside the document.ready function, so loads every page, however it seems like this is wrong an uneccessary use of resource.

I'd be interested to know if there is a better approach to registering this code on such an adhoc basis.

0

2 Answers 2

1

You can use the ScriptManager class to register scripts in the load of your user control. This can also be used to ensure that the script isn't loaded more than once.

You can use either RegisterClientScriptBlock or RegisterClientScriptInclude depending on whether you want to provide the script in-line or have it in a separate file.

Sign up to request clarification or add additional context in comments.

Comments

0

Use the ScriptManagerProxy in the user control to reference this script. That will include it on your page without having to have it on all pages.

This requires a ScriptManager or ToolkitScriptManager (if using AjaxControlToolkit) on your master page or content page. You can only have one script manager per entire rendered page.

http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanagerproxy.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.