OK after days of searching i am still getting nowhere. zero knowledge of javascript is not helping. I currently have content coming from the following javascript: <script language='JavaScript' src='http://www.website.com/feedjs.php?src=1.xml&mode=wi'> </script> I am trying to replace all instances of a term coming from the javascript: i.e. in php i use: $html = str_replace("Apple", "Orange", $html); What can i use to translate all instances of Apple to orange in the above javascript. Is it even possible. Will add rep for anyone who can solve it for me.
Sorry for being a total noob, but i don't think this works for an external js does it? (Or i couldn't get it to work - I have tried inserting that inside the header, and inside <script language='JavaScript' src='http://www.website.com/feedjs.php?src=1.xml&mode=wi'> </script> with no luck.) If the content is called by the external javascript: <script language='JavaScript' src='http:// www.website.com/feedjs.php?src=1.xml&mode=wi'> </script> and the contents of http:// www.website.com/feedjs.php?src=1.xml&mode=wi is in the format: document.write(1); document.write('</td><td>'); document.write('<img src="http://www.website.com/mainheader.gif">'); document.write('</td><td>'); etc etc How or where do i do it? Thanks
In that case, I would do the conversion in feedjs.php, or is that a remote file? I guess you could do the replace on the innerHTML of the content container (preferably not <body>): <html> <head> <title>test</title> <script type="text/javascript"> function replace_all(search, replace) { var html = document.getElementById ? document.getElementById('container') : document.all['container']; while (html.innerHTML.indexOf(search) != -1) html.innerHTML = html.innerHTML.replace(search, replace); } </script> </head> <body onload="replace_all('apple', 'orange')"> <div id="container"> apple apple apple </div> </body> </html> Code (markup):
If your server has PHP abilities, you might be able to add a page by example 'feedjs.php'. That page gets the javascript codes, and replaces all the instances you would like, and then it is returned to the HTML page (you use a external javascript to feedjs.php) If you don't have PHP, however you might be able to use krt's solution; though, that one won't work if there are also alert boxes in the software
KRT, thanks your solution works, but it is a bit slow as i need to replace lots of text. Devianthans3 - How would i do what you have suggested, as it seems it would get 'processed quicker', although i do not really understand how to achieve it. Also on a related point, how would i be able to strip links (<href) from the external javascript. Would that be possible with any of the above 2 solutions? Thanks!
Ok, the other solution is to use server side scripting to get the output of feedjs.php and do all the work on the server, then output to the browser. Create a file, e.g. test.php <?php // get feed data // $data = "document.write('apple <a href=test>asfg<a href=test>acfg</a></a>');"; // this was for testing purpose $data = file_get_contents('http://www.website.com/feedjs.php?src=1.xml&mode=wi'); // remove document.write calls $data = str_replace(array("document.write('", "');"), '', $data); // apples to oranges $data = str_replace('apple', 'orange', $data); // strip links $regex = '/<a(.+)href=(.+)>(.+)<\/a>/U'; do { $data = preg_replace($regex, '$3', $data); } while (preg_match($regex, $data)); // output echo $data; PHP:
krt thanks i think this is definetely more of what i am looking for. And the php looks alot more familiar than the JS. But still a small / or maybe large? problem. Am i able to link to the external JS? i.e. how do i (or is it possible) to make $data = <script language='JavaScript' src='http://www.website.com/feedjs.php?src=1.xml&mode=wi'> </script> As i do not have access to the remote external js / feed. is it possible or am needing to stick with the body onload. Thanks
Please see the change I made to the code in the last post, that should get the data from the external JS. Change website.com to the actual site of course. Note that the code I used uses file_get_contents() and some web hosts disable users from using this. Let me know if this is the case and I will give you a alternative solution using cURL.