问题描述
我正试图在没有进行任何服务器调用的情况下从textarea中获取单词换行符。 让我90%的方式。问题是,每次单击提交按钮时,页面都会在iframe内重新加载。这是代码:
I'm trying to get the word wrapped line breaks from a textarea without having make any server calls. This answer gets me 90% of the way there. The problem is that the page is reloaded inside of the iframe each time the submit button is clicked. Here's the code:
<html><body>
<script type="text/javascript">
function getURLParameter(qs, name) {
var pattern = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( pattern );
var res = regex.exec( qs );
if (res == null)
return "";
else
return res[1];
}
function getHardWrappedText() {
var frm_url = document.getElementById('frame').contentDocument.URL;
var text = unescape(getURLParameter(document.getElementById('frame').contentDocument.URL, 'text')).replace(/\+/g,' ');
return text;
}
function onIFrameLoad() {
var text = getHardWrappedText();
console.log(text);
}
window.onload = function() { console.log("loading") } ;
</script>
<form name="form" method="get" target="frame">
<textarea id="text" name="text" cols=5 wrap="hard">a b c d e f</textarea>
<input type="submit">
</form>
<iframe id="frame" name="frame" onload="onIFrameLoad()" style="display:none;"></iframe>
点击提交按钮即可输出:
Clicking on the submit button gives the output:
loading
a b c d e
f
有没有办法阻止iframe重新加载页面?
Is there a way to prevent the iframe from reloading the page?
推荐答案
您必须将false返回给表单的onsubmit事件。
You have to return a false to the onsubmit event of the form.
< form name =formmethod =get target =frameonsubmit =onIFrameLoad>
如果它基于某些您不想加载的条件
If it is based on some condition that you do not want to load
function onIFrameLoad() {
var text = getHardWrappedText();
console.log(text);
if(text.indexOf('\n')<0) return false;
}
我只是随意添加 text.indexOf ('\ n')< 0
,你可以有任何有意义的东西或总是返回false(看起来不太可能)
Am just putting a random thing at text.indexOf('\n')<0
, you can have anything that makes sense or always return false (which looks unlikely)
这篇关于html - 从textarea获取包装文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!