Dynamically adding new <script> element.

View: New views
6 Messages — Rating Filter:   Alert me  

Dynamically adding new <script> element.

by ccoroom :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

There's unexpected result when I tried to add new <script> element.

Here's a code snippet:
  page.executeJavaScript("var script = document.createElement('script')");
                page.executeJavaScript("script.setAttribute('type', 'text/javascript')");
                page.executeJavaScript("script.text = 'var execSpec = 42;'");
                page.executeJavaScript("document.head.appendChild(script);");
                page.executeJavaScript("document.head.removeChild(script);");

                System.out.println(page.executeJavaScript("window.execSpec").getJavaScriptResult());

The result should be '42', but it returns 'undefined'. It works fine on web browsers.

Actually, this is a part of Mootools library code, "core/Browser.js":
 function $exec(text){
        if (!text) return text;
        if (window.execScript){
                window.execScript(text);
        } else {
                var script = document.createElement('script');
                script.setAttribute('type', 'text/javascript');
                script.text = text;
                document.head.appendChild(script);
                document.head.removeChild(script);
        }
        return text;
 };

I'm working on HtmlUnit 2.1. Is it fixed?
I cannot try this on head version.
Head is fail to package because of failed test.

Regards,
Sung-Ahn Kim.

Parent Message unknown Re: Dynamically adding new <script> element.

by Ahmed Ashour :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Dear Sung,
 
The following case fails with FF2, it gives 'document.head has no properties'.
 
BTW, just recently: "JavaScript: window.execScript() now accepts undefined 'language' (IE only)." Because in the specs, execScript should have another string language.
 
Anyway, running the below case with head HtmlUnit works correctly.
 
Please advise if you have an issue.
 
<html>
<head>
<script>
  function test() {
    execMe('alert(1)');
  }
  function execMe(text){
    if (!text) return text;
    if (window.execScript){
        window.execScript(text);
    } else {
        var script = document.createElement('script');
        script.setAttribute('type', 'text/javascript');
        script.text = text;
        document.head.appendChild(script);
        document.head.removeChild(script);
    }
    return text;
  }
</script>
</head>
<body>
<input id=myId type=button onclick='test()' value='Test'>
</body>
</html>
----- Original Message ----
From: ccoroom <ccoroom@...>
To: htmlunit-user@...
Sent: Saturday, June 28, 2008 5:31:00 PM
Subject: [Htmlunit-user] Dynamically adding new <script> element.


Hi,

There's unexpected result when I tried to add new <script> element.

Here's a code snippet:
          page.executeJavaScript("var script = document.createElement('script')");
        page.executeJavaScript("script.setAttribute('type', 'text/javascript')");
        page.executeJavaScript("script.text = 'var execSpec = 42;'");
        page.executeJavaScript("document.head.appendChild(script);");
        page.executeJavaScript("document.head.removeChild(script);");

   
System.out.println(page.executeJavaScript("window.execSpec").getJavaScriptResult());

The result should be '42', but it returns 'undefined'. It works fine on web
browsers.

Actually, this is a part of Mootools library code, "core/Browser.js":
function $exec(text){
    if (!text) return text;
    if (window.execScript){
        window.execScript(text);
    } else {
        var script = document.createElement('script');
        script.setAttribute('type', 'text/javascript');
        script.text = text;
        document.head.appendChild(script);
        document.head.removeChild(script);
    }
    return text;
};

I'm working on HtmlUnit 2.1. Is it fixed?
I cannot try this on head version.
Head is fail to package because of failed test.

Regards,
Sung-Ahn Kim.
--
View this message in context: http://www.nabble.com/Dynamically-adding-new-%3Cscript%3E-element.-tp18171285p18171285.html
Sent from the HtmlUnit - General mailing list archive at Nabble.com.


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Htmlunit-user mailing list
Htmlunit-user@...
https://lists.sourceforge.net/lists/listinfo/htmlunit-user


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Htmlunit-user mailing list
Htmlunit-user@...
https://lists.sourceforge.net/lists/listinfo/htmlunit-user

Re: Dynamically adding new <script> element.

by gredler :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

As Ahmed said, please try HEAD. If you're having trouble with the unit tests, you can skip the tests via Maven (-DskipTests with the latest versions). See the Maven docs for more info.


 
On Sat, Jun 28, 2008 at 10:31 AM, ccoroom <ccoroom@...> wrote:

Hi,

There's unexpected result when I tried to add new <script> element.

Here's a code snippet:
               page.executeJavaScript("var script = document.createElement('script')");
               page.executeJavaScript("script.setAttribute('type', 'text/javascript')");
               page.executeJavaScript("script.text = 'var execSpec = 42;'");
               page.executeJavaScript("document.head.appendChild(script);");
               page.executeJavaScript("document.head.removeChild(script);");


System.out.println(page.executeJavaScript("window.execSpec").getJavaScriptResult());

The result should be '42', but it returns 'undefined'. It works fine on web
browsers.

Actually, this is a part of Mootools library code, "core/Browser.js":
 function $exec(text){
       if (!text) return text;
       if (window.execScript){
               window.execScript(text);
       } else {
               var script = document.createElement('script');
               script.setAttribute('type', 'text/javascript');
               script.text = text;
               document.head.appendChild(script);
               document.head.removeChild(script);
       }
       return text;
 };

I'm working on HtmlUnit 2.1. Is it fixed?
I cannot try this on head version.
Head is fail to package because of failed test.

Regards,
Sung-Ahn Kim.
--
View this message in context: http://www.nabble.com/Dynamically-adding-new-%3Cscript%3E-element.-tp18171285p18171285.html
Sent from the HtmlUnit - General mailing list archive at Nabble.com.


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Htmlunit-user mailing list
Htmlunit-user@...
https://lists.sourceforge.net/lists/listinfo/htmlunit-user



--
Daniel Gredler
http://daniel.gredler.net/
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Htmlunit-user mailing list
Htmlunit-user@...
https://lists.sourceforge.net/lists/listinfo/htmlunit-user

Re: Dynamically adding new <script> element.

by ccoroom :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sorry, Ahmed.
I missed important portion of the mootools library code.
They define 'document.head' but the htmlunit cannot recognize it.

And, unfortunately it doesn't work on htmlunit with FF2 configuration.
(It works fine on IE configuration)

Oh, I'm working on htmlunit 2.1 and 2.2-snapshot.

Here's my code:
----
<head>
        <script type="text/javascript" src="../Source/Core/Core.js"></script>
        <script type="text/javascript" src="../Source/Core/Browser.js"></script>
        <script type="text/javascript">
                function test() {
                        $exec.call($exec, 'var execSpec = 42');
                        alert(execSpec);
  }
        </script>
</head>
<body>
<input id=myId type=button onclick='test()' value='Test'>
</body>
</html>
----

You can see the mootools code from the repository (Core.js, Browser.js):
http://github.com/mootools/mootools-core/tree/master/Source/Core

I know to see the external link is quite bothering you. Sorry, but its content somewhat large to paste.


Regards,
Sung-Ahn Kim.


Ahmed Ashour wrote:
Dear Sung,
The following case fails with FF2, it gives 'document.head has no properties'.
BTW, just recently: "JavaScript: window.execScript() now accepts undefined 'language' (IE only)." Because in the specs, execScript should have another string language.
Anyway, running the below case with head HtmlUnit works correctly.
Please advise if you have an issue.
<html>
<head>
<script>
  function test() {
    execMe('alert(1)');
  }
  function execMe(text){
    if (!text) return text;
    if (window.execScript){
        window.execScript(text);
    } else {
        var script = document.createElement('script');
        script.setAttribute('type', 'text/javascript');
        script.text = text;
        document.head.appendChild(script);
        document.head.removeChild(script);
    }
    return text;
  }
</script>
</head>
<body>
<input id=myId type=button onclick='test()' value='Test'>
</body>
</html>

----- Original Message ----
From: ccoroom <ccoroom@gmail.com>
To: htmlunit-user@lists.sourceforge.net
Sent: Saturday, June 28, 2008 5:31:00 PM
Subject: [Htmlunit-user] Dynamically adding new <script> element.


Hi,

There's unexpected result when I tried to add new <script> element.

Here's a code snippet:
          page.executeJavaScript("var script = document.createElement('script')");
        page.executeJavaScript("script.setAttribute('type', 'text/javascript')");
        page.executeJavaScript("script.text = 'var execSpec = 42;'");
        page.executeJavaScript("document.head.appendChild(script);");
        page.executeJavaScript("document.head.removeChild(script);");

   
System.out.println(page.executeJavaScript("window.execSpec").getJavaScriptResult());

The result should be '42', but it returns 'undefined'. It works fine on web
browsers.

Actually, this is a part of Mootools library code, "core/Browser.js":
function $exec(text){
    if (!text) return text;
    if (window.execScript){
        window.execScript(text);
    } else {
        var script = document.createElement('script');
        script.setAttribute('type', 'text/javascript');
        script.text = text;
        document.head.appendChild(script);
        document.head.removeChild(script);
    }
    return text;
};

I'm working on HtmlUnit 2.1. Is it fixed?
I cannot try this on head version.
Head is fail to package because of failed test.

Regards,
Sung-Ahn Kim.
--
View this message in context: http://www.nabble.com/Dynamically-adding-new-%3Cscript%3E-element.-tp18171285p18171285.html
Sent from the HtmlUnit - General mailing list archive at Nabble.com.


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Htmlunit-user mailing list
Htmlunit-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/htmlunit-user




-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Htmlunit-user mailing list
Htmlunit-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/htmlunit-user

Parent Message unknown Re: Dynamically adding new <script> element.

by Ahmed Ashour :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Dear Sung,
 
>>They define 'document.head' but the htmlunit cannot recognize it.
 
We have a case now, please open a bug report to track the progress.
 
Html/js:
 
<html>
<head>
<script>
  function test() {
    execMe('alert(1)');
  }
  function execMe(text){
    if (!text) return text;
    if (window.execScript){
        window.execScript(text);
    } else {
     document.head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.setAttribute('type', 'text/javascript');
        script.text = text;
        document.head.appendChild(script);
        document.head.removeChild(script);
    }
    return text;
  }
</script>
</head>
<body>
<input id=myId type=button onclick='test()' value='Test'>
</body>
</html>
Yours,
Ahmed


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Htmlunit-user mailing list
Htmlunit-user@...
https://lists.sourceforge.net/lists/listinfo/htmlunit-user

Parent Message unknown Re: Dynamically adding new <script> element.

by Ahmed Ashour :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Dear Sung,
 
It has been fixed in SVN, please give the latest snapshot a try and advise if you have any other issue with MooTools.
Thanks a lot,
Ahmed
 
----- Original Message ----
From: Ahmed Ashour <asashour@...>
To: htmlunit-user@...
Sent: Sunday, June 29, 2008 5:12:37 PM
Subject: Re: [Htmlunit-user] Dynamically adding new <script> element.

Dear Sung,
 
>>They define 'document.head' but the htmlunit cannot recognize it.
 
We have a case now, please open a bug report to track the progress.


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Htmlunit-user mailing list
Htmlunit-user@...
https://lists.sourceforge.net/lists/listinfo/htmlunit-user
LightInTheBox - Buy quality products at wholesale price!