Muffinator
Muffinator

Reputation: 143

The name "document" does not exist in the current context, in the layout page

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

Upvotes: 1

Views: 4915

Answers (2)

Christos
Christos

Reputation: 53958

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# command. 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>
}

Upvotes: 1

CodingIntrigue
CodingIntrigue

Reputation: 78545

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>
    }
}

Upvotes: 7

Related Questions