0

I don't know if this possible so I apologize in advance if this question comes off as being a little ignorant. I am working a website that must use a template and server-side languages such as PHP are disallowed. There is an external JS file on the website that I would like to modify but I cannot figure out how to access/modify it from the main script using JQuery.

In case that was not clear, here is an example:

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="fileToModify.js"></script>
<title>Untitled Document</title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>code to modify external file goes here</script>
</body>
</html>

In the above example, I would like to be able to place code (in JQuery/JS/some other language like that) where it says "code to modify external file goes here" and alter the local copy of "fileToModify.js" (for example, to delete a function within it). I am aware that, if successful, such a deletion could not alter the real file hosted on the server; I am only trying to alter the local copy when the user downloads it to change how it behaves after the page loads).

Would such a task be possible with JQuery/JS/something of the sort?

Thanks for your help

6
  • you cannot modify a file on a server with javascript alone. You could fetch the file contents and put into a string and modify it and then eval() it but that could cause issues. Commented Jan 28, 2014 at 3:42
  • @PatrickEvans I know - as stated above, I would like to modify the local copy of it after the browser downloads the external JS file (not the real file hosted server-side) Commented Jan 28, 2014 at 3:43
  • javascript does not have access to the temporary files, or file system for that matter. There is the File System Api but its not widely implemented and i do not know if it even lets you to request access to those areas. Commented Jan 28, 2014 at 3:45
  • @PatrickEvans Oh. That is what I was asking. Thanks for telling me that, good to know and unfortunately means that my task will probably be impossible Commented Jan 28, 2014 at 3:45
  • Well you could do what i suggested grab the contents of the js file through ajax modify them and then eval them, but again like i said that could open up security holes if not handled properly. Commented Jan 28, 2014 at 3:47

1 Answer 1

7

You can use ajax to get the contents of the file (and not execute it), then do use javascript to replace some of the data. Then you could use eval to execute it.

$.ajax({
  method: 'GET',
  dataType: 'text',
  url: 'to-modify.js'
}).then(function(data) {
  data = data.replace('bar();', '')
  eval(data)
})

Live demo (click)

Sign up to request clarification or add additional context in comments.

2 Comments

That would be most helpful if you could make a demo :)
Thank you for the helpful demonstration, it should be most useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.