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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace script code

Status
Not open for further replies.

WebStuck

Programmer
Apr 12, 2003
79
US
Hi,

I want to use a script at the top of an html page to change a line in script code at the bottom of the page. I want to do this without changing any of the bottom script code such as adding an id= or onclick=. I have been trying to do this by using onload in the body tag to call my script changing function but can't figure out how to reference the bottom script code through the DOM. If needed, I could put an id= in a tag before the bottom script code such as:

<td id="script_code_to_change>
<script>
line_to_change
</script>
</td>

If someone could give me the best way to reference the bottom script code and change the line_to_change, it would be greatly appreciated.

Thanks!
Ben
 
WebStuck,

Give instead the id="script_code_to_change" to the script tag, for instance. Then use getElementById("script_code_to_change").innerHTML to alter the whole script there.

regards - tsuji
 

tsuji - maybe you didn't see this:

WebStuck said:
I want to do this without changing any of the bottom script code such as adding an id=

Ben - perhaps you could tell us what this is for, as there may be an alternative method. For example, if it is to bypass auto-added JS code (GeoCities, etc), then other methods may be possible.

Hope this helps,
Dan

 

Incidentally, trying to use innerHTML to overwrite a script block in IE produces JS errors... so it might consider it a security risk. If that weren't the case, I was going to propose using:

Code:
document.getElementsByTagName('script')[1].innerHTML = newScript

I think the best bet would be if the names of the functions in the bottom script section were known... then you could use something like this:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		function replaceBottomScriptFunction() {
			var newScript = 'alert(\'New fake bottomScriptFunction running!\')';
			bottomScriptFunction = new Function(newScript);
		}
	//-->
	</script>
</head>

<body>
	Click <a href="javascript:bottomScriptFunction();">here</a> to run &quot;bottomScriptFunction&quot;
	<br />
	Click <a href="javascript:replaceBottomScriptFunction();">here</a> to replace &quot;bottomScriptFunction&quot;

	<script type="text/javascript">
	<!--
		function bottomScriptFunction() {
			alert('Real bottomScriptFunction running!');
		}
	//-->
	</script>
</body>
</html>

Hope this helps,
Dan

 
I think the problem is that the top code needs to change the bottom script code as the browser is parsing it, not after as the onload call is doing. However, I want the bottom script code in place to run if a visitor doesn't have javascript enabled. I only want the bottom script changed if javascript is enabled so it can be changed. Any ideas on how to do this?

Thanks!
Ben
 

I want the bottom script code in place to run if a visitor doesn't have javascript enabled

And how do you suggest that the browser will run this script if the user has scripting disabled?

Have you considered using a noscript tag at all to provide different content for non-scripting users?

Hope this helps,
Dan

 
Let me recap what I want since I made an error in what I said in one of my posts:

I want a script at the top of a html page that can change a line in a script at the bottom of the page. The problem I think is that the top script can't change the bottom script code after the bottom script has already been processed. So, is there some way to have the top script change the bottom script as it is being processed?

Thanks!
Ben
 

Unless the bottom script calls the top script, there would be no way of the top script knowing if the the bottom script is mid-execution or not. It would only be able to alter it. You could put script-altering code at the top of the page to alter the bottom script, but this would only be useful if that had already been parsed / processed - which it may not have done at that stage (scripts, AFAIK, are parsed / processed in source order).

Furthermore, if you want to alter the bottom script, why include it as-is anyway?

Can you clarify why you need to alter the bottom script? Do you have control over it? What is this really for?

Dan

 
The code at the bottom of the page is gotten from another company, which is not allowed to be changed. However, the company has given me 2 different versions of the code, neither which are allowed to be changed, but the difference between the 2 codes is just 1 line. So, I am trying to use the 1 version of the code for certain site visitors and the other version for other people that come to my site. Does this make sense?

Thanks!
Ben
 
You can document.write() JS files into a page, and you could use an if statement to write one or the other at the bottom, depending on what happened at the top.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top