Dynamically change the HTML source using native JavaScript?

可紊 提交于 2019-12-08 10:43:24

问题


I was trying to change my HTML files. Please see this to get a better understanding of what I am up to.

Every thing worked fine but as I viewed the source it was not changed. The changes I was trying to make were only reflected on the web-page I had opened on my browser.

Is it by any way possible to change the actual source of the HTML page?

I am using IE8 and GreaseMonkey4IE to run my JavaScript on the web pages I want to manipulate, just in case you are interested.

There is a similar question here. But I have my HTML files at my local storage. No server side - client side technicalities.

EDIT 1

Well I do have setup Tomcat but it is all in my local machine.
Also if I can modify the DOM can't I save it as a new HTML file, this way I won't have to mess with the original source file.

This is how we do it in firefox. There must be some way to do it in IE8.

EDIT 2
Now it is working fine for the HTML pages without any frames. But when I am trying this on pages with frames it is not working.It is going to kill me i am sure. Perhaps this is because frame access other HTML pages. For example :

<frameset rows="95,*" frameborder="NO" framespacing="0" border="0" marginwidth="0"  marginheight="0">
    <frame name="title" target="content" src="Strategy%20Details.asp_files/title.htm"  scrolling="NO">
 <frameset cols="168,*">
   <frame name="navigation" noresize="noresize" width="168" target="content" src="Strategy%20Details.asp_files/navigation.htm">
   <frame name="content" noresize="noresize" src="Strategy%20Details.asp_files/home.htm">
 </frameset>
 </frameset> 



See how the frames are accessing the HTML pages in src attribute.

Any Idea ???


回答1:


You should be able to see the dynamic changes using IE Developer Tools.

When you "View Source" the page shows you the source that was delivered from the server without any changes made afterwards. IE Developer Tools (or Firebug in Firefox, or Dragonfly in Opera) will let you see changes dynamically (and will also highlight changes as they occur!)




回答2:


If I understand you correctly you want to change the contents of the HTML file on your harddrive? That's not possible using JS, and regardless of where the HTML file is (local machine or not), there's always a server side and client side. Basically your OS is acting as the server (providing you with HTML) and your browser is still just a client, with no more rights than it would have if you were on a page on the web.

It sounds like you should look in to some kind of server technology that allows for dynamic web pages such as PHP, JSP, server side JS, or any of a million others.




回答3:


So from looking at what you are doing with Firefox, you don't want javsctipt to do the saving, just be able to modify the DOM using script the Save the HTML representation of the changes in IE.

IN IE, use IE developer tools. Press f12. If you've manipulated the DOM, click the refresh icon in developer tools. Then Click the save icon.




回答4:


Edit: No there isn't a way, atleast not without inspecting source without firebug.




回答5:


It isn't possible. I suppose it depends on the browser, but when you request the source of a page, they usually perform the request again, or use a cached result of the original source (evidently before client manipulation, which only exists and makes sense in your view of the page in your browser), so you get what you have in the source (in this case the HTML files in your local storage).



来源:https://stackoverflow.com/questions/7568343/dynamically-change-the-html-source-using-native-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!