Rachel
Rachel

Reputation: 103587

How to check that this event would occur after DOM is ready in jquery?

I want to get page_tag information from the page and want to make sure that DOM for this page is already ready before getting the page tag information.

I am doing

$(document).ready(
{
   alert("test");
   var page_tag : $("head meta[name='page_tag']").attr('content');
   page_tag : (page_tag) ? page_tag : '';
}

But it gives me errors,

missing : after property id
alert("Check if document is ready");\n

Any suggestions on what could be the possible reasons for it or any other way of checking if the dom is ready or not before getting page_tag information.

Upvotes: 3

Views: 109

Answers (3)

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827932

You have a syntax error, you are using : instead of = to make the assignment:

var page_tag = $("head meta[name='page_tag']").attr('content');
page_tag = (page_tag) ? page_tag : '';

Or simply:

var page_tag = $("head meta[name='page_tag']").attr('content') || '';

The above will work, because the attr method returns a String or undefined if the attribute is not present.

Upvotes: 2

Mitch Dempsey
Mitch Dempsey

Reputation: 39939

I'm pretty sure it should be:

$(document).ready(function() {
   alert("test");
   var page_tag = $("head meta[name='page_tag']").attr('content');
   page_tag = (page_tag) ? page_tag : '';
}

You need to use a = instead of :

Upvotes: 0

jessegavin
jessegavin

Reputation: 75690

try

$(document).ready(function() {
   var page_tag = $("head meta[name='page_tag']").attr('content');
   alert(page_tag);
});

The ready() function requires you to pass in a function that it will execute when the document is ready.

Upvotes: 4

Related Questions