简体   繁体   English

Javascript:如何发布整个页面?

[英]Javascript: how to post the whole page?

Is it possible to post the whole html page to the server(assuming that the server can handle it)? 是否可以将整个html页面发布到服务器(假设服务器可以处理)?

I'm trying to implement the following setup. 我正在尝试实施以下设置。 The user makes some changes(the user selects some text, and this text is wrapped in <span> tags with some class) to html and submits the page. 用户对HTML进行一些更改(用户选择了一些文本,然后将这些文本包装在带有某些类的<span>标记中)以提交html并提交页面。 The server extracts some relevant changes from a page. 服务器从页面中提取一些相关的更改。 Is this possible? 这可能吗? Or I need to design some more complex scheme? 还是我需要设计一些更复杂的方案?

Well you need to do some Javascript magic to do this. 那么,您需要执行一些Javascript魔术才能做到这一点。

Normaly only Input elements are submitted. 通常只提交输入元素。

In your case you need to use Javascript to read the DOM and convert that to HTML ( document.body.innerHTML might work ) and then either use Ajax to call the server or store the data in a textbox and submit the form from the script. 在您的情况下,您需要使用Javascript读取DOM并将其转换为HTML( document.body.innerHTML might work ),然后使用Ajax调用服务器或将数据存储在文本框中并从脚本提交表单。

But the question is, to do the change, wrap a seletion in span, you already have a script doing this, could you just post the action, for example parent element + selection start and selection end, or possibly the entire text within the element to the server and have the server duplicate the action? 但是问题是,要进行更改,请在跨度中包装一个选择,您已经有一个脚本在执行此操作,您是否可以发布操作,例如父元素+选择开始和选择结束,或者元素中的整个文本到服务器,并让服务器重复该操作?

It is quite possible - document.getElementsByTagName('html')[0].innerHTML should get you the page's HTML, and XMLHttpRequest (AJAX) allows POST for sending data. 很有可能document.getElementsByTagName('html')[0].innerHTML应该可以获取页面的HTML,而XMLHttpRequest (AJAX)允许POST发送数据。 Combining these two should get you the basic functionality. 将这两者结合起来将为您提供基本功能。

What you wish to do is possible, but IMHO you need to take a different approach. 您希望做的是可能的,但是恕我直言,您需要采用其他方法。 Rather than trying to post the whole page back, you would be better off presenting all the content of the page inside an HTML form. 与其尝试将整个页面回发,不如将页面的所有内容都呈现在HTML表单中。 Each of the elements can be styled using CSS so that they don't look like form fields, but they remain editable. 可以使用CSS设置每个元素的样式,以使它们看起来不像表单字段,但仍可编辑。 You could also switch areas within from static HTML to a regular form looking element using Javascript. 您还可以使用Javascript将区域从静态HTML切换为常规形式的外观元素。

Using AJAX techniques you can automatically post changes on blur rather than having to have the user 'Save' on each change. 使用AJAX技术,您可以在模糊时自动发布更改,而不必每次更改都让用户“保存”。

PS Post method has no built-in transfer limitations: 1MB, 100MB or the entire Web. PS Post方法没有内置的传输限制:1MB,100MB或整个Web。 But usually there are some reasonable limits set on the server-side. 但是通常在服务器端设置一些合理的限制。

http://bytes.com/topic/javascript/answers/92044-size-limit-submit-using-post http://bytes.com/topic/javascript/answers/92044-size-limit-submit-using-post

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM