I am trying to proxy a function so that I just replace part of the page instead of reloading the whole page.
<div id="content" style="display:none;">
</div>
<div class='contentitem' id='restart' style="margin-top: 100px; margin-left: 100px; margin-bottom: 100px; display:block;">
<table>
<tr>
<td></td>
<td><p style="font-size: 1.2em; font-weight: bold;"><%=translate([==[Loading...]==])%></p></td>
</tr>
</table>
</div>
<script type="text/javascript">
//jquery library
// released under BSD license
</script>
<script type="text/javascript" src="/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
var content = $("#content");
$(document).ready(function(){
content.load('/cgi/b/sfltr/cfg/ div .contentitem', function(content) {
$(this).find('tbody').each(function(){
$("span:contains('Content Based Filtering')").closest("table").remove();
});
document.getElementById("restart").style.display = "none";
document.getElementById("content").style.display = "block";
//alert('Load was performed.');
});
});
//intelligent version of submitform: will only refresh what is needed:
(function() {
// proxying...
var proxied = submitForm; // keep a copy of original method
submitForm = function(theForm, actionNo, flags, primkey, confirmMsg, anchor, eval_code, customP) {
//potential issue with the switch language... I think actionNo = 0 means reload.
//alert('gotcha');
// the following code is identical to what is in util.js
//customP is only for restore page to upload config file to GW
var VALIDATE = 0x01;
var CONFIRM = 0x02;
var EVAL = 0x04;
if (flags && (flags & VALIDATE))
if (!validate(theForm, actionNo)) return false;
if (flags && (flags & CONFIRM))
if (!confirm(confirmMsg)) return false;
if (flags && (flags & EVAL) && eval_code)
eval(eval_code);
if (actionNo > 0)
theForm.elements[0].value = actionNo;
if (primkey)
theForm.elements[1].value = primkey;
var customArgs = new String("");
if (g_state[TYPE].length > 0)
{
customArgs += "name="+encodeURIComponent(g_state[NAME]);
if (g_state[KEY].length > 0)
customArgs += "&" + g_urlArgs[KEY] + "=" + encodeURIComponent(g_state[KEY]);
}
if (g_state[TID].length > 0)
{
if (customArgs != "")
customArgs += "&";
customArgs += g_urlArgs[TID] + "=" + encodeURIComponent(g_state[TID]);
}
theForm.action += constructArgString(customArgs);
if (customP)
theForm.action += "&" + customP;
if (anchor)
theForm.action += "#" + anchor;
//end of copy paste...
//theForm.submit();
//disable_fields(theForm);
document.getElementById("restart").style.display = "block";
document.getElementById("content").style.display = "none";
//Now, I change the submit
//theForm.submit(function () {
$.ajax({
type: $(theForm).attr('method'),
url: $(theForm).attr('action'),
data: $(theForm).serialize(),
datatype: "html"
})
.done(function (html) {
//alert(html);
var Test = -1;
$("html").find('p').each(function(){
//var result = $("this").find(":contains('You are being redirected')"); //check if tries to redirect
Test = Test?Test:this.indexOf("You are being redirected"); //if we already found we skip.
//TBD portuguese variant...
alert(this);
alert(Test);
});
if (Test==-1) {
content.html(html);
content.find('table').each(function(){
$("span:contains('Content Based Filtering')").closest("table").remove();
// Filtragem com base em conteúdo
$("span:contains('Filtragem com base em conte')").closest("table").remove();
$(document).find("#restart").css("display","none");
content.css("display", "block");
});
}
else
{
GoAndRemember('/PC_ov.lp', '');
}
})
.fail(function (xhr, textStatus, errorThrown) {
alert('Unknown error:'+xhr.responseText);
window.location.reload();
});
}
return false;
})();
</script>
When I submit the form using the function, the post works, and the 200OK contains the page I want to parse.
What I don't understand is:
- Why I start loading the embedded script after (script of the code embedded in the 200OK), => Probably it is because I set this inside the div before I filter it out, but I thought it was the only way to make it. Should I create a new XHR object to store it in and then filter that object and set the DIV with the resulting .html()? If so could someone give a small example?
- why I end up with an error because it overwrites the JavaScript I am using (it should be exactly the same code anyway)
Thanks