3

I am attempting to register some javascript in my view, and I seem to be having an issue. In my master page, at the bottom I have:

@(
    Html.Telerik().ScriptRegistrar()
        .OnDocumentReady(@<text>
            // Open the hidden window when the feedback-link is clicked
            $('#feedback-link').click(function(e) {
                e.preventDefault();
                $('#FeedbackWindow').data('tWindow').center().open();
            });
        </text>)
)

In my view, I want some view specific javascript, so I have:

@(Html.Telerik().ScriptRegistrar().OnDocumentReady(
    @<text>
        // Upon contact selection change, update the contact sidebar summary
        $('#contactlist').change(function() {
            alert('Selected id' + $(this).val());
        });
    </text>)
)

Unfortunately, this is causing my view's javascript from being declared both in the MVC view, and in my master page when the final page is rendered. How can I get this to only register the script once?

1

1 Answer 1

4

As I said in my forum reply the ScriptRegistrar is being output twice because told so. The @() Razor expression will output its contents whereas @{ } will be executed. In your case you need to use @{ } for the specific script:

@{ Html.Telerik().ScriptRegistrar().OnDocumentReady(
    @<text>
        // Upon contact selection change, update the contact sidebar summary
        $('#contactlist').change(function() {
            alert('Selected id' + $(this).val());
        });
    </text>);
}

Also note that the @{ } block requires a semi-colon.

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

1 Comment

Ohhh, that makes sense, I was reading the posts on it, and I kept reading it as @{ } should be over the master pages. Guess I was getting it backwards :). I'll give this a shot when I get home.

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.