2

I am using Knockoutjs in my asp.net MVC-5 application. I have the following javascript in view:

<script type="text/javascript">

    var model = "@Html.Raw(Json.Encode(Model))";

    $.get("@Url.Action("_CityPartial")" ...)

    //any much more code using similar Html helpers + pure javacsript code.

</script>

Now i want to know, is there any way to transfer this javascript code in a separate js file (keeping Html helpers as it is).

I want to transfer javascript code to separate file because i dont want any user to check my javascript code (using chrome inspect element or any other way).

If the transfer is not possible than please let me know if there is a any way to minifiy the javascript in view itself ??

1
  • there is no such thing as security in JavaScript/Html. Since it will be transferred to client, user can view it as they want. Keep the critical code in server itself, whenever that calculation/data required make an ajax request to server Commented Jan 10, 2014 at 10:43

3 Answers 3

2

You could create an external .js file with your code in and pass your serialized json object to it like this:

<script type="text/javascript">
    var model = @Html.Raw(Json.Encode(Model));
    DoThis(model);
</script>

This has the benefits of keeping the main body of javascript in a separate file.

Any other razor variables can be passed across to the methods defined in the javascript in the same manor as the model has been above.

However as Stanyer mentions this is still javascript and it will run on the client.

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

Comments

0

You can load it via an external JavaScript file, but unfortunately as JavaScript is a client-side scripting language regardless of whether its loaded inline or externally, the client can still view the code which is being executed on their browser.

You mention minifying - again this can still be interpreted by a client if they really wanted to see your code, but there are many tools online which can minify your JavaScript for you.

Examples:

2 Comments

Actually he can't move it as simple as you describe it, as he uses inline asp.net code in it
You could store the data which uses the ASP.net output into JavaScript variables the main script and call the JavaScript variables in the external file. I don't understand the down vote as I've provided an answer to his comment i dont want any user to check my javascript code
0

No you cant keep the @Html helpers in external javascript file. They are all server side syntax and will be rendered in your HTML page inline.

What maximum you can do is, assign it in a var variable in your page and refer it inside a external page.

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.