form POST in iframe without affecting history

前端 未结 5 880
野的像风
野的像风 2020-12-09 20:27

Is it possible to submit a form inside an iframe without affecting the browser\'s history?

I\'ve implemented sending a cross domain POST request. It uses Javascript

5条回答
  •  没有蜡笔的小新
    2020-12-09 21:24

    (?) I don't see a good reason that you are using an iframe element. (Since this was an older question, maybe you were testing in 1997-vintage HTML 4. BTW, ignore the "XML" in the object name - it's a carry over from the year 2000 and later versions of [X]HTML - no XML required.)

    If you send the HTTP request from JavaScript rather than from elements in the document, nothing will be added to the history.

    function httpPost(body) {
      req = new XMLHttpRequest();
      req.open("POST", "/submit-here", true/*async*/);
         // or "http://some-other-domain.com/submit-here"
      req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      req.send(body);
    }
    window.onload = function() {
       document.getElementById('button').onclick = function() {
          httpPost('x=Some+Value&y=Another+Value&z=' + new Date().getTime());
          // or use a buildQuery function, JQuery formSerialize, etc. to create the body
       }
    }
    

    I use symbolic links ("ln -s ...") so that the forms can be submitted to the same domain as the document, so in my case the "/submit-here" is a relative URI.

提交回复
热议问题