Asked  7 Months ago    Answers:  5   Viewed   31 times

How do I execute some JavaScript that is a string?

function ExecuteJavascriptString()
    var s = "alert('hello')";
    // how do I get a browser to alert('hello')?



With the eval function, like:

eval("my script here");
Tuesday, June 1, 2021
answered 7 Months ago

If you are working with the form, you can use onsubmit event.

Using jQuery you can do that with

$('#myform').submit(function() {
  // your code here
Wednesday, July 28, 2021
answered 5 Months ago

Without global pollution and avoids the non-strict arguments.callee method.

Based on this answer.

// This Immediately Invoked Function Expression is invoked via `call`.
// call() takes 1 or more arguments: the first being the scope you want the 
// invoked function be in; the other arguments become arguments of the function
// itself.
// In this case, the scope of our function is an empty object ({}).
// This object allows us to attach functions using the this.* syntax and call the 
// functions with this["string"]() without ever polluting the global namespace. 
// It is also forward compatible with ECMAScript 5's scrict-mode as it does not
// use arguments.callee.
(function(document, window){
    var functionName = "myFunction";

    // you'll need to define the function _before_ you call it.
    this.myFunction = function(){
        //Do some stuff


}.call({}, document, window));
Sunday, October 3, 2021
Jake Stevenson
answered 2 Months ago

I was able to fix this issue by making browser sleep for few seconds after every change in URL.

Below is the code snippet:

67 it('should create new job listing', function () {
68        //Login As Admin To Access Vacancies Feature
69        loginAsManager();
.        //load manager's dashboard list page
.        dashboardPage = new DashboardPage();
.        //load vacancies list page
.        var vacanciesUrl = browser.baseUrl + '#/vacancies';
.        browser.sleep(2000);
.        expect(browser.getCurrentUrl()).toEqual(vacanciesUrl);
.        vacanciesPage = new VacanciesPage();

I don't think this is a neat solution to this problem.

I will be happy to hear, if someone has a better solution to this.

Cheers Gaurav

Friday, October 15, 2021
Morrison Chang
answered 2 Months ago

Maybe a dependency error. I'll suggest you to use a better practice for such lib.

  1. Put the fullcalendar scripts into vendor/assets/javascripts

  2. Require this script manually in application.js before the tree and after jquery and any other lib it depends.

    /=require 'fullcalendar`


Check the loaded Javascript files in header, if application.js is the last, you have dependency error. The lib must be there before calling it.

Thursday, November 11, 2021
Yuriy Faktorovich
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :