Is it possible to overwrite internal javascript? I am working for a non profit as an internship and they want to escape a template that almost all of the non profits in that organization have to use, the catch is the template is written in Javascript if it is possible to over ride it please tell me as I really need to know ASAP.

my be this would(i have not tired it). Javascript code are written in <script></script> tags. So search these tags in the document by using: var sTag = document.getElementsByTagName("script"). This will give u an array of script tags. Now over write them by using sTag[0].innerHTML = "your javascript code";


If this work please let me know?

my be this would(i have not tired it). Javascript code are written in <script></script> tags. So search these tags in the document by using: var sTag = document.getElementsByTagName("script"). This will give u an array of script tags. Now over write them by using sTag[0].innerHTML = "your javascript code";


If this work please let me know?

I am sorry I do not completly understand, are you saying to look in the edocument for the code you listed? If so the code isn't there if you mean something else please pm me or post.

I am sorry I do not completly understand, are you saying to look in the edocument for the code you listed? If so the code isn't there if you mean something else please pm me or post.

Okay I have searched the whole javascript and they do not use the var command at all they use javascript in a odd way just for images. If there is a way to overwrite it I would be happy and so would my boss.

Hi tiger86, if u can explain ur problem little bit more, i want to know what exactly u want to do.

var is not a command but a way to declare a variable in javascript.

I am explaining my prev. post again, if u still did not get ur problem solved, explain ur problem in detail.

first i want to tell u what i understand from ur post: You want to overwrite the javacript at run-time. if this is correct then my post will help u otherwise explain me ur problem. here is want i explained in prev post. for example u have a page:

<html>
<head></head>

<body>
<script> 
  function myfunct() {
   //do something
  }
</script>

</body>
</html>

if u want to change js code in above page,
this a js code todo that :

var tags = document.getElementsByTagName("script");

//Now changing the js code
tags[0].innerHTML = "your new js code should be written here as a string";

again i had not tested it. please let me know how it works?

It works!

the code luckychap posted works but I would not suggest using it unless it is your own website and you are not connected to a national website.

Ok, and what if we have a code like this?

<script src="example.js" type="text/javascript"></script>

In this case, how can we replace the code loaded with example.js?

Let me be more informative. There is this issue on one site - the script it loads via src attribute in script tag is cut down by the server, and instead of having a 362kb successfully loaded script, I've something like 160kb cut one. So I'm trying to write a Greasemonkey userscript, which can overwrite the cut down script with code that it has in itself. For now, I'm not having any success with this problem. Can anyone help me with this?

As I have explained how to get <script> tags. Say we have an array of tags 'scripts'.

var orginalSrc = "example.js", newSrc = "new.js";
// Looping through all the tags
for(var i = 0; i < scripts.length; i++) {
    if(scripts[i].src == originalSrc) {
           scripts[i].src = newSrc;
           break;
    }
}

Is that what u r looking for?

As I have explained how to get <script> tags. Say we have an array of tags 'scripts'.

var orginalSrc = "example.js", newSrc = "new.js";
// Looping through all the tags
for(var i = 0; i < scripts.length; i++) {
    if(scripts[i].src == originalSrc) {
           scripts[i].src = newSrc;
           break;
    }
}

Is that what u r looking for?

Will it redefine all the functions introduced in the old script?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.