I am using Selenium version 1, with PHP. I need to execute a large Javascript code snippet. When I use the runScript() function, it gives me this error:
Request URI Too Large
How can I handle this? Or is it possible to include it as an external js file but run it as well at once? I have tried using the addScript() function, that contains my js code, but it did not work. I have also tried to include my all code in a function like this:
function executeMyCode() {
// all my code here
}
And then include it with addScript(), and then use runScript() like this: $this->selenium->runScript('executeMyCode()').
But it did not work either.
Thanks.
Related
I'd like to load and execute a Javascript function in external Javascript file using Node.js and similarly retrieve back with desired result after execution. Is it possible/not possible? If not, any other alternatives.
Any help regarding this is greatly appreciated.
Yes, you can load another JS file as a module using Node.js module system and function require. There's quite good description of how to do it in Node.js documentation, take a look int this page.
Basically you have to load your external file calling function require and passing path to that file:
var externalFunction = require('./path/to/external/file')`;
but to make this work you have to export that function by writing something like
module.exports = functionToExport;
in your external file, where functionToExport is the name of the function which you want to use.
I don't want to give my code to client. I know there is no any such way which make my code 100% secure from client or editing by other developers but at least i think from may clients i can hide my code, for this i am using the following way:
For example, I have the following code:
$php = base64_encode('<?php function add($a,$b){echo($a+$b);} add(30,40); ?>');
The above line produces the following code of my php code:
PD9waHAgZnVuY3Rpb24gYWRkKCRhLCRiKXtlY2hvKCRhKyRiKTt9IGFkZCgzMCw0MCk7ID8+
I used gzencode() method to compress the above generated code like this:
$txt = "PD9waHAgZnVuY3Rpb24gYWRkKCRhLCRiKXtlY2hvKCRhKyRiKTt9IGFkZCgzMCw0MCk7ID8+";
$ph = gzencode($txt,8);
I can get the Original php code by using the following code:
echo(base64_decode($ph));
It produces:
<?php function add($a,$b){echo($a+$b);} add(30,40); ?>
Now, this code shows in the source code in the browser but i want to run this code like the original php code, so, what i have to do to make this executable?
Thanks
You can use eval to run the string as inline code. Check this (eval)
You can also create an anonymous function which your string code should be inside (create_function)
I am attempting to build a PHPMailer based email system for a basic website.
This is the line of code that is giving me trouble:
$xajax->printJavascript('xajax/');
Now, this is the tutorial I am using.
Regarding the above line of code, the tutorial says this:
How to use the code inside a webpage?
Place the form (variable), the function (and the includes) before of all html code. Next we need to include some JavaScript file in the documents HTML header (place also php tags):
$xajax->printJavascript('xajax/');
When I run all of the code (including: PHPMailer script; Ajax script), I get this error, on the aforementioned line of code.
Fatal error: Call to a member function on a non-object
So, my question is, do I need to in someway customize this code or make it run to a filepath of some ajax core file or something?
I would be willing to be $xajax is not defined. Try adding this after including the libraries but before including the call to that method:
$xajax = new xajax();
There may be some additional setup required by xajax. A more complete code example could help troubleshoot.
Suppose i need to invoke a function (php) from inside a js function using AJAX, is the
following syntax correct:
$.post("phpReceivingFile.php", {func: (name: "james")}
....
....
No, it won't work, you can't invoke specific php functions like that. I suggest sending a get parameter and inside your php do something like:
if($_GET['func'] == 'yourfunction') yourfunction($_GET['name']);
You might want to have a look at this short php/ajax tutorial to see how you get the variables sent to a php script.
I was viewing a Appcelerator Titanium Video Tutorial and I saw they used syntax like
<script type="text/php">
...
global $window, $document;
mysql_connect(...) or die $window->alert('...');
$document.getElementById('xxx');
...
</script>
so I have a few questions. Is it any difference if I use <?php ?>
without setting $window and $document - won't they be "unset" variables?
I guess I can use (basic, not jQuery for example) Javascript functions like alert and getElementById() in PHP too?
In this case, they can only be "undefined", not "unset". However, they have "global" prefix, so there is a chance they are defined in some other code, possibly even outside your file.
$window->alert(...) probably outputs HTML that reads as javascript alert or a similar function.
You cannot use javascript alert() in PHP, because it is not PHP function.
As far as PHP is concerned, there is no JavaScript — only text.
$window and $document are just variables defined elsewhere in the PHP. $window appears to be an object with some methods that output text (text that happens to include JS syntax) while $document appears to be a string.
They will be undefined if they haven't been defined already.
You can write any JS function you like as normal text. If you want to use an object to generate it, then you need to have an object that is aware of that function.
For a regular web app, the code you're showing makes no sense whatsoever, because PHP runs on the server and JavaScript runs on the client. PHP is used to build the HTML code which forms the DOM tree on which JavaScript functions like getElementById() operate, so it's completely impossible to use them meaningfully within PHP code.
However, a cursory investigation reveals that Appcelerator Titanium is a sort of runtime that is meant to run applications using web technology completely on the client. In such a runtime, it is possible that the PHP code is running in the context of an already complete HTML DOM and interacts with it via JavaScript-like bridge functions. But that's completely different from how PHP normally works.