javascript - The name "document" does not exist in the current context, in the layout page - Stack Overflow

admin2025-04-09  0

I'm trying to change the layout page based on a cookie so i tried doing this:

 function LoggedOrNot()
    {
        @if (Page.User.Identity.IsAuthenticated)
        {
            var x = document.cookie;
            document.getElementByID("signupbutton").innerHTML = x;
        }
    }

but it's not recognizing document

I'm trying to change the layout page based on a cookie so i tried doing this:

 function LoggedOrNot()
    {
        @if (Page.User.Identity.IsAuthenticated)
        {
            var x = document.cookie;
            document.getElementByID("signupbutton").innerHTML = x;
        }
    }

but it's not recognizing document

Share Improve this question asked Aug 18, 2015 at 7:07 MuffinatorMuffinator 1423 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

That's because Razor thinks you're still writing C# code. Use <text> to tag it as plain text:

function LoggedOrNot()
{
    @if (Page.User.Identity.IsAuthenticated)
    {
        <text>var x = document.cookie;
        document.getElementByID("signupbutton").innerHTML = x;</text>
    }
}

You should wrap it in text tags.

<text>var x = document.cookie; document.getElementByID("signupbutton").innerHTML = x</text>

This is because here you define a block of razor code and the ViewEngine, when try to execute the View see this like a c# mand. Apparently, in this context there isn't any variable called document. Furthermore, you need there to embed some js code. The way to do so, is to wrap it into text tags.

@if (Page.User.Identity.IsAuthenticated)
{
    <text>var x = document.cookie; 
    document.getElementByID("signupbutton").innerHTML = x</text>
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744202480a235864.html

最新回复(0)