Is there a way to execute external Javascript from PHP when Javascript is disabled client side?
I am using PHP to generate a dynamic web form that uses an external (to my site) javascript file to set a hidden form input value. My form submits to my own PHP page before reposting to an external site. The external site requires this hidden value for any submission.
Many of the browser that are using my form do not have javascript enabled, so the tracking fails and I can not submit to the external site. Is there any method of executing the javascript file to get the tracking information from my php code without sending it to the clients browser?
This is a pseudo code mock up of the code that I want to handle the form's post:
if ($doPOST) {
//Check POSTed parameters for tracking id ($tid)
if ($tid == ''){
//Execute external javascript to get tid
}
//Post form data to external site
}
I have looked around and could not find any relevant information, but if you know of a site where this is explained let me know. For clarification, I am not looking for information about to incude a javascript file client side.
Edit: This is the Javascript executed by the form. I have no power to change this script. 开发者_运维问答The number 48891 is not hardcoded in the script it changes with execution:
onReady=(function(ie){var d=document;return ie?function(c){var n=d.firstChild,f=function(){try{c(n.doScroll('left'))}catch(e){setTimeout(f,10)}};f()}:/webkit|safari|khtml/i.test(navigator.userAgent)?function(c){var f=function(){/loaded|complete/.test(d.readyState)?c():setTimeout(f,10)};f()}:function(c){d.addEventListener("DOMContentLoaded",c,false)}})(/*@cc_on 1@*/);
var test_track=function(data){onReady(function(){document.getElementById('test_track').value = data.TID})}
test_track({"TID" : "488891"});
Yes, you can execute JavaScript server-side. However, it wouldn't make much sense just for a snippet, and you wouldn't be able to access the DOM. See the v8cgi project for a server side JavaScript implementation.
Just rewrite it in PHP.
No. If JavaScript is disabled on the clients browser you cannot get info out of the browser. I suggest you use PHP for tracking
If you are under time pressure (I am not saying this is a good idea), you can parse out the TID value and write it in the hidden form value 'test_track'.
You can get the content of the javascript with:
$js = file_get_contents('http://example.com/source.js');
精彩评论