Click here to Skip to main content
14,544,858 members
Rate this:
Please Sign up or sign in to vote.
Hi, everyone.

I've been struggling with my javascript functions as they execute on page load and I can't seem to figure out why.

After my @page, @model, @using and @inject I added my javascript code wrapped within head.
I know the function isn't being called anywhere else in the code because I wrote a brand new function I never plan on using other than to test what could be causing the issue during debug.

Here is my code:
@page
@model ////model


<head>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script>

            function test() {
                @{

            Console.WriteLine("test function");} 

            }
    

    </script>


</head>


What I have tried:

I tried searching for any parenthesis causing the issue, but found nothing.
I deleted everything else on my page just to ensure the function isn't being called from some other code.

I also tried wrapping the function in head
Posted
Updated 21-May-20 20:53pm
v3
Comments
ZurdoDev 21-May-20 16:37pm
   
If you put JavaScript code in a script tag and it is NOT in a function, it will run. Otherwise, something has to call the function for it to actually run.
AO1411 21-May-20 16:54pm
   
But it is a function and still executes on page load?
j snooze 21-May-20 17:22pm
   
Probably a dumb question on my part but if its regular javascript why do you have this

function test() {
@{

Console.WriteLine("test function");}

}

instead of

function test() {

Console.WriteLine("test function");
}

I thought the @ symbol tells the page its c# code, javascript shouldn't need that to execute. Granted I have not played much with the new blazor stuff.


....nevermind, perhaps you are trying to run c# code in a javascript function??
AO1411 22-May-20 1:50am
   
@j snooze
Yes in my "normal" code I am using AJAX to call a controller side function. But even when I don't use C# it still seems to run.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

It runs every time because you're running the client AND THE SERVER on your DEV machine at the same time.
The @Console.WriteLine("..."); runs when the SERVER processes the page, NOT when the javascript runs.

You're seeing the message of that output in the Output (Debug) window in Visual Studio, correct? If it was the javascript code outputting that message, you wouldn't see it in Visual Studio.

To see the output of actual javascript code running, you would replace the above line with simply console.log("...");.
function test() {
    console.log("test function");
}

The output of that code would be in the Console window in the BROWSER, not Visual Studio. Hit F12 in the browser to see the debug console there.
   
Comments
AO1411 22-May-20 2:09am
   
My original code uses an ajax function to call a controller. But the thing is it must only call the controller after the user pressed a button. For the ajax url I used the @Url.Action() C# method. Is this therefore not possible?
Richard Deeming 22-May-20 6:45am
   
Using @Url.Action in your Razor view will emit a string into the markup which is sent back to the user. Using that string as the URL for an AJAX request is a perfectly reasonable approach, and will not cause your Javascript function to execute without being called.

Perhaps you should update your question with the actual code you're having problems with.
AO1411 22-May-20 7:26am
   
I believe your solution made me understand what is going on. Thanks!
Dave Kreskowiak 22-May-20 12:01pm
   
Razor is a template engine. The code you put in there does NOT get executed on the client. It's executed on the server to build the HTML that is sent back to the client.

I think that's where you're having problems understanding what's going on.

You might want to step through the code as it executes on your DEV machine AND what what is happening in the browser in the F12 tools there at the same time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100