Well, When you are building an ASP.NET MVC most of the time we use PartialViews to better manage our project by increasing re-usability of views. The most common problem that has been reported by ASP.NET Developers is that JavaScript does not work in Partial Views and Yes They are right.

However there is always a solution to every problem we face and today in this post I will give a solution on how to use javascript in Partial Views.

Just in your script tags, define a new function as follow:

function runWhenReady() {
if (window.$)
scriptToExecute();
else
setTimeout(runWhenReady, 100);
}

Now in the scriptToExecute Function you may write any javascript you want to run as follow:

function scriptToExecute() {
alert('Hello');
    }

That’s it now just call your javascript function runWhenReady(); and the final code will be as follow:

<script type="text/javascript">

    function scriptToExecute() {
alert('Hello');<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>
    }

    function runWhenReady() {
        if (window.$)
            scriptToExecute();
        else
            setTimeout(runWhenReady, 100);
    }
    runWhenReady();
</script>

Happy Coding 🙂