简体   繁体   English

使用document.write修复html

[英]Using document.write for fixed html

I'm creating a webform that has combos containing 100 values or so. 我正在创建一个包含包含100个左右值的组合的Webform。 The values are the same. 值是相同的。

The form may have several records. 该表格可能有几条记录。 So if there are 100 records there are 10,000 lines which seems pretty wrong from a "download" point of view. 因此,如果有100条记录,则有10,000行,从“下载”的角度来看,这似乎是错误的。

The thing is: I want to keep that combo dynamic to keep the id from the database. 事情是:我想保持该组合动态以保持数据库中的ID。

So I came up to the following: 所以我想出了以下几点:

  .....
 <script>
      stupidCombo = "<option>a"+
                    "<option>b"+
                    ...
                    "<option>99zzz"+
                    "</select>";
 </script>
 ..... form here
 .... for each item in huge list do paint <table><tr> etc. etc 

 <td>
 <select name="comb-<%=id%>">
      <option selected><%=obj.val%>
      <script>document.write(stupidCombo);</script>
 </td>
... close form, table, html etc. 

I have rendered it and "look" fine. 我已经渲染它并“看起来”很好。 The page has decreased from 50k lines to 5k and the select is created by the javascript on the client side. 页面从5万行减少到5万行,并且选择是由客户端上的javascript创建的。

My question is..... 我的问题是.....

Is this ok? 这个可以吗?

Is there any risk involved? 有没有涉及的风险?

I borrow this Idea after understanding who most of the javascript frameworks work, but most of them work on a <div> element rather that just to the document it self. 在了解了大多数javascript框架的工作原理之后,我借用了这个想法,但是它们大多数都在<div>元素上工作,而不仅仅是对文档本身进行工作。

I'm targeting IE6 and this is a quick fix that has to be on production tomorrow morning ( so I don't want to spend too much time on this ) but I don't want to have 50,000 lines written for each request if I can help it. 我的目标是IE6,这是一个快速修复程序,明天早上必须投入生产(因此,我不想花太多时间),但是如果我希望每个请求都写50,000行,可以帮上忙。

Thanks 谢谢

HTTP compression (mod_gzip, etc.) will compress all that nicely. HTTP压缩(mod_gzip等)将很好地压缩所有内容。

If you insist on document.write , you will have to use JavaScript to write <select> element as well, because in HTML you're not allowed to put <script> inside <select> . 如果坚持document.write ,则还必须使用JavaScript编写<select>元素,因为在HTML中,不允许将<script>放在<select>

Another approach is to send one copy of the control to the browser and then duplicate it with help of selectElement.cloneNode(true) . 另一种方法是将控件的一个副本发送到浏览器,然后在selectElement.cloneNode(true)帮助下进行复制。

好吧,如果禁用了javascript,则显然无法正常工作。

HTML produced by document.write works in same way as normal one, so there is no any technical problems with your solution. document.write生成的HTML的工作方式与普通HTML相同,因此您的解决方案没有任何技术问题。 ( of course, such solution works only if javascript enabled ) (当然,这种解决方案仅在启用了javascript的情况下有效)

By the way, for the lists with big count of options, you can look at "suggest box" component , which can replace native selectbox. 顺便说一句,对于具有大量选项的列表,您可以查看“建议框”组件 ,该组件可以替换本机选择框。

Google does it ("document.write") all the time (Analytics/Adsense/...), so I don't see why there would be anything wrong with it. Google一直(Analytics / Adsense / ...)都这样做(“ document.write”),所以我不明白为什么会有什么问题。

Your solution does look a bit odd, because the <script> tag is inside a <select> tag, so you better check in several browsers. 您的解决方案确实有些奇怪,因为<script>标记位于<select>标记内,因此最好在多个浏览器中签入。 After all, you never know what IE is going to do :) 毕竟,您永远都不知道IE会做什么:)

Update. 更新。

I've done just the way I asked and it worked fine. 我按照我的要求进行了操作,效果很好。 There were no problems with the js inside ie. 里面的js没有问题。

But... 但...

Once the table is rendered, the next thing the user attempt to do ( always the user does something unexpected ) was.. 呈现表格后,用户尝试做的下一件事(总是用户做意外的事情)。

"Ok, the report looks fine. I'm going to copy/paste it into MS-Excel thank you." “好的,报告看起来很好。我将其复制/粘贴到MS-Excel中,谢谢。”

Which is something that I don't really care. 我真的不在乎。 Is up to the user, but the result was: The excel spreadsheet die ( or froze which is almost the same)!!!! 由用户决定,但结果是:excel电子表格死了(或冻结了,几乎是一样的)!!!! because the javascript engine within the excel is not as good as it is inside IE, the combos took forever to copy and actually it leave excel application unusable. 由于excel中的javascript引擎不如IE内部的javascript引擎好,因此该组合花了很长时间才被复制,实际上使excel应用程序无法使用。

I didn't knew the copy/paste was so good from IE to Excel that it actually copied all the generated html and Excel tried to run the javascript too with terrible results. 我不知道从IE到Excel的复制/粘贴效果如此之好,以至于它实际上复制了所有生成的html,而Excel也尝试运行javascript并产生了可怕的结果。

I'll try again by leaving the raw html and see if that works better. 我将保留原始html,再试一次,看看效果是否更好。

:( :(

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

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