Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change body tag

Status
Not open for further replies.

WebRic

Technical User
Sep 21, 2004
95
GB
Hi all,

is there a way to re-write the body tag?

The tag currently looks like this:
Code:
<body id="someidgoeshere">
or just
Code:
<body>

I have a site wide external jscript file that I'd like to use to change the body tag to
Code:
<body id="someidgoeshere" ondragstart="return false" onselectstart="return false">
or just
Code:
<body ondragstart="return false" onselectstart="return false">

Thanks,

Richard
 
Tags for the most part can be changed via something like this.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<script type="text/javascript">

function bodychange(){
	document.getElementById('mybody').id = "newid";
	alert(document.getElementById('newid').id);
	alert(document.getElementById('newid').ondragstart)
	document.getElementById('newid').ondragstart = "return";
	alert(document.getElementById('newid').ondragstart)
}
</script>
	<title>Untitled</title>
</head>

<body id="mybody" onload="bodychange()">

test

</body>
</html>
 
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<script type="text/javascript">

function bodychange(){
	document.getElementsByTagName('body')[0].id = 'someidgoeshere';
	document.getElementById('someidgoeshere').ondragstart="return false";
	document.getElementById('someidgoeshere').onselectstart="return false";
	alert(document.getElementById('someidgoeshere').ondragstart);
	alert(document.getElementById('someidgoeshere').ondragstart);
}
</script>
	<title>Untitled</title>
</head>

<body id="mybody" onload="bodychange()">

test

</body>
</html>
Edited a bit.
 
Acctually i think I'm wrong you may have to use
object.addEventListener(); FIrefox
object.attachEvent(); IE
 
Hopefully i didn't confuse you. Forgot that events needed to be added differntly than properties.
I think ondragstart is ie only, so this works for ie.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<script type="text/javascript">
function rfalse(){
	return false
}

function bodychange(){
    document.getElementsByTagName('body')[0].id = 'someidgoeshere';
   var bodyobj = document.getElementById('someidgoeshere');
   bodyobj.attachEvent("ondragstart", rfalse);
   bodyobj.attachEvent("onselectstart", rfalse);

}
</script>
    <title>Untitled</title>
</head>

<body id="mybody" onload="bodychange()">
fdsafdsafdasfasfasfsadfsafafsafdsa

</body>
</html>
 
Hi j4606,

My problem is that I don't really want to go through all of the pages of my website and adjust the body tag (there are hundreds of pages).

So I thought there may be a short cut that I could do and just add some code into the my site.js file which is referenced from every page in my site already.

Is there a way of editing the function that you've provided so that I would not nead the onload="bodychange()" bit?

Thanks,

Richard
 
Can you simply copy out the guts of the above bodychange() and include it at the top of the .js file outside of a function (like in the area where you may declare global variables), so that it will execute on every page?
 
Hi CLFlava,

Within my body tag the id changes so I'd made some adjustments to j4606's code and include your suggestion. Such that it look like:

Code:
function rfalse(){
    return false
}

function bodychange(){
		var bodyobjid = document.getElementsByTagName('body')[0].id;
		var bodyobj = document.getElementById(bodyobjid);
		bodyobj.attachEvent("ondragstart", rfalse);
		bodyobj.attachEvent("onselectstart", rfalse);
}

onload = bodychange;

Unfortunately it doesn't seem to work. Do you know what I've done wrong?

Richard
 
Can't for the life of me get it to work :(
Here's the relevant bit of code in the javascript file
Code:
function rfalse(){
    return false
}

function bodychange(){
    var b = document.getElementsByTagName("body")[0];
    b.ondragstart = rfalse;
    b.onselectstart = rfalse;
}

onload = bodychange;

I don't get any errors so I'm finding it hard to troubleshoot... any ideas?


R
 
this works well for me:

file.html
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]


<html>
<head>
<script type="text/javascript" src="file.js"></script>
    <title>Untitled</title>
</head>

<body id="mybody">
fdsafdsafdasfasfasfsadfsafafsafdsa
</body>
</html>

file.js
Code:
function rfalse(){
    return false;
}

function bodychange(){
   var bodyobj = document.getElementsByTagName('body')[0];
   bodyobj.ondragstart = rfalse;
   bodyobj.onselectstart = rfalse;
}

onload = bodychange;



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Weird still doesn't seem to work for me. THink I'll create an exact replica of the above then see if I can figure out what difference is causing the problem.

Thanks for your help with this.

Richard
 
Well if you use dreamweaver or some other web development program you can do a replace on every file in a certain folder... thats what i would do in this case... just replace

<body id="someidgoeshere">

or

<body>

with whatever you want...


Jason

Jason

[red]Army[/red] : [white]Combat Engineer[/white] : [blue]21B[/blue]

 
Hi Richard,

I assume you never got this to work. The reason mine worked was likely because I simply had text in a body tag. The reason yours likely didnt work is because you had text within subelements inside your body tag.

Is this functionality that you consider important? If not, I wouldnt worry about it. if so, you may have to loop through each element and provide the same function calls for the same events to each.



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Hi cLFlaVA,

It wasn't hugley important so I put it on the back burner.
Thanks for reason why though.


Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top