首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将Javascript重定向到页面并动态修改它

将Javascript重定向到页面并动态修改它
EN

Stack Overflow用户
提问于 2017-05-23 19:06:11
回答 1查看 450关注 0票数 0

我想做的是:

  1. 我点击一个按钮继续,带我到另一个html页面。
  2. 然后,我将根据我的数组创建一个动态表。

但佩奇没有显示表格。注意:我只想通过JS实现这一点,而不是Jquery或任何其他框架。

代码:

当我按下“继续”按钮时,将调用以下函数

代码语言:javascript
运行
复制
function Proceed(){

    window.location.href = '\cart.html';

    function addItems(){

        var cartTable = document.createElement("table");

        var tr1 = document.createElement("tr");

        tr1.appendChild(createElement("td"));
        tr1.appendChild(createElement("td"));
        tr1.appendChild(createElement("td"));

        tr1.cells[0].appendChild( document.createTextNode('Sr. Number'));
        tr1.cells[0].appendChild( document.createTextNode('Item'));
        tr1.cells[0].appendChild( document.createTextNode('Price'));

        cartTable.appendChild(tr1);

        for(i=0; i<cartCount; i++){

            var cartItem = myCart[i];

            var tr = document.createElement("tr");

            tr.appendChild(createElement("td"));
            tr.appendChild(createElement("td"));
            tr.appendChild(createElement("td"));

            tr.cells[0].appendChild( document.createTextNode(i+1));
            tr.cells[0].appendChild( document.createTextNode(cartItem.Name));
            tr.cells[0].appendChild( document.createTextNode(cartItem.Price));

            cartTable.appendChild(tr);

        }

        document.getElementByID(finalCart).innerHTML = cartTable;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-05-23 21:19:48

您不能离开页面导航,然后继续在您刚刚离开的页面上运行脚本。

相反,您可以首先生成新页面的html,然后将其转换为您可以重定向或链接到的dataURI。(因为这是在一个非常小的片段中,所以我不能针对一个新窗口或自动地重定向到它,所以我只是生成一个直接链接;在现实生活中,您不会有这个限制。)

代码语言:javascript
运行
复制
function Proceed() {

  /* since this is all going to be converted to a string anyway,
  it would be easier to just create the html as a string instead
  of using DOM methods. In the interest of not mucking around too 
  much with your existing code I'm leaving that as is and then 
  extracting the html string with .outerHTML. */

  var cartTable = document.createElement("table");
  var tr1 = document.createElement("tr");
  tr1.appendChild(document.createElement("td"));
  tr1.appendChild(document.createElement("td"));
  tr1.appendChild(document.createElement("td"));
  tr1.cells[0].appendChild(document.createTextNode('Sr. Number'));
  tr1.cells[1].appendChild(document.createTextNode('Item'));
  tr1.cells[2].appendChild(document.createTextNode('Price'));

  cartTable.appendChild(tr1);
  /* Etcetera for remainder of table */

  // create the data URI:
  var generatedLink = "data:text/html;base64," + window.btoa(cartTable.outerHTML);

  // create a link to it:
  document.getElementById('output').innerHTML = "<a href='" + generatedLink + "'>Go to generated page</a>";
}
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="Proceed()">Generate Link</button>

<div id="output"></div>

(不用说,您可能最好将这个服务器端生成为一个普通的HTML页面,但是如果您需要保留客户端,这应该是可行的。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44143113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档