...

View Full Version : Add meta tag to <head>



safhac
03-11-2009, 01:18 PM
Hi
I have been struggling with this for a while, Iv seen the solutions posted here.
http://www.codingforums.com/showthread.php?t=30133

That didnt work
The code is added to the oScript element but thats it.
I have tried another script from here:

http://www.codingforums.com/showthread.php?t=118809

I want to add a meta tag from the body tag and it worked using Jscript

This worked only on my local but not on hoster:


HtmlMeta GoogleVerify = new HtmlMeta();
GoogleVerify.Name = "verify-v1";
GoogleVerify.Content = "681rsArfYmoRz88BIT8ZVDeLk4b6VMEn1+StcF61iwY=";
HtmlHead head = (HtmlHead)Page.Header;
head.Controls.Add(GoogleVerify);


And on the hoster theres a scripting error as well

why innerHTML doest work?

safhac
03-12-2009, 09:33 AM
Due to the lack of responses I assume that if the aforesaid solutions didnt work then theres no other way to do it.

Iv'e tried all the other suggestions adding the links but to no avail.
They dont work on any browser:confused:

I guess this is where we admit defeat

freedom_razor
03-12-2009, 09:58 AM
Works in FF3 and IE7:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<script type="text/javascript">
//- <![CDATA[
var yourTag=document.createElement('meta');
yourTag.name = "myTag";
yourTag.content = "ohYEAH";
document.getElementsByTagName('head')[0].appendChild(yourTag);
// and check if it works
for (i=0;i<document.getElementsByTagName('meta').length;i++){
document.write(document.getElementsByTagName('meta')[i].content+'<br />');
}
//- ]]>
</script>
</body>
</html>

safhac
03-14-2009, 09:14 AM
Hi
Thanx for your post.
Your script works and I see the meta tags including the new one are written on the page yet the new tag is not in the <head>

This happened to me with the previous examples as well. The new tag is added to the <head> object child array but is not added into the page..

Is there a pre-document-init event in js?
Maybe the reason is that when the script is parsed the head has already been loaded..??

BTW the script writes in ff yet ie has a syntax error..

Philip M
03-14-2009, 09:50 AM
Hi
Maybe the reason is that when the script is parsed the head has already been loaded..??


I guess that is it. And is there not possibly a security aspect?

freedom_razor
03-14-2009, 12:48 PM
Code I posted didn't throw any errors in IE7. But it wasn't working well in IE either, because NAME attribute wasn't included in new META tag in IE.
I've corrected this now, and you can see that the new META tag is in HEAD section:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<script type="text/javascript">
//- <![CDATA[
var yourTag=document.createElement('meta');
window.getSelection ? yourTag.name='your name' : yourTag.Name='your name' //all lower-case doesn't work in IE7, and only all-lowercase works in FF
yourTag.content='your content';
document.getElementsByTagName('head')[0].appendChild(yourTag);
alert('Contents of HEAD section:'+document.getElementsByTagName('head')[0].innerHTML);
//- ]]>
</script>
</body>
</html>

If you hoping to see that META in source code of the page you'll have to open a new document and fill it with contents of the page+that new META tag. Usually, if page is modified client-side, the changes aren't reflected in the source code you view with your browser, though they are applied to the document.

Can I ask you what possible use you see for adding META tags client-side?

Actinia
03-14-2009, 12:56 PM
I tried this in FF using the Web Developer toolbar. This lets you view the generated code (View Source, View Generated Code), which indeed shows the generated meta tag just before the closing </head>. The Developer Toolbar in IE7 also shows it.

John

safhac
03-14-2009, 09:39 PM
Thanx freedom_razor

The script is working perfectly both in IE and FF

I can see it with firebug but not in source..

I wanted to index my video sharing blog in google

Ive asked one of the blog developers and he said thats impossible

but if i can get it to work using script then fine

so the script you wrote works but google doesnt verify it for indexing

I have to find another way..

freedom_razor
03-14-2009, 09:49 PM
What do you mean, your blog doesn't come up at all? How old is the domain?

Also, Javascript is a client-side technology, it is not going to help you at all with Google. It works in your browser. Bots reach documents on the server but usually don't execute Javascript in them.

Why don't you just put META in the source? You have no access to it?

safhac
03-15-2009, 08:49 AM
no i dont

if i did then there weer no problem..

youre right i got to have server side code to add it with script thats why the jscript worked in the 1st example..

Cheers



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum