Watch, Follow, &
Connect with Us

Welcome, Guest
Guest Settings
Help

Thread: JavaScript errors in TCppWebBrowser


This question is answered.


Permlink Replies: 2 - Last Post: Feb 23, 2017 1:40 PM Last Post By: Rolf Fankhauser Threads: [ Previous | Next ]
Rolf Fankhauser

Posts: 19
Registered: 9/3/01
JavaScript errors in TCppWebBrowser  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 23, 2017 11:10 AM
Hi all,

I use a TCppWebBrowser component in my application to inform the users about news. The loaded html page is located on my hosted web server.
Now I wanted to renew my html page with some html5 code. First I tried the <details> element but it's not supported by IE (supported by Firefox and Chrome). Then I found an accordion with similar function. It uses CSS styles and JavaScript. I got an error message that the function document.getElementsByClassName is not supported.
If I load the page directly in IE11 all is fine. No problem in Firefox and Chrome.
Next, I replaced document.getElementsByClassName by a function getElementsByClassName2. Works in all browsers but not in TCppWebBrowser.
I get an error that the property "toggle" of an undefined element cannot be called.

I don't understand why it works in IE directly but not in TCppWebBrowser. I thought TCppWebBrowser uses an ActiveX element from the present OS.

I use XE5. IE11, Win7.

Thanks!
Rolf

This is the test html-page:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Accordion</title>
<style>
 /* Style the buttons that are used to open and close the accordion panel */
button.accordion {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    text-align: left;
    border: none;
    outline: none;
    transition: 0.4s;
}
 
/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
button.accordion.active, button.accordion:hover {
    background-color: #ddd;
}
 
/* Style the accordion panel. Note: hidden by default */
div.panel {
    padding: 0 18px;
    background-color: white;
    display: none;
}
</style>
 
<noscript>JavaScript is deactivated! </noscript>
 
 
</head>
<body><br>
<h2>Accordion</h2>
 
<button class="accordion">Section 1</button>
<div class="panel">
  <p>öüäèéàLorem ipsum...</p>
</div>
 
<button class="accordion">Section 2</button>
<div class="panel">
  <p>Lorem ipsum...</p>
</div>
 
<button class="accordion">Section 3</button>
<div class="panel">
  <p>Lorem ipsum...</p>
</div>
<script>
var getElementsByClassName2 = function( searchClass, domNode, tagName) { 
   if (domNode == null) domNode = document;
   if (tagName == null) tagName = '*';
   var el = new Array();
   var tags = domNode.getElementsByTagName(tagName);
   var tcl = " "+searchClass+" ";
   for(i=0,j=0; i<tags.length; i++) { 
      var test = " " + tags[i].className + " ";
      if (test.indexOf(tcl) != -1)
         el[j++] = tags[i];
   } 
   return el;
} 
</script>
 
<script>
var acc = getElementsByClassName2("accordion");
var i;
//alert(acc[1].className);
for (i = 0; i < acc.length; i++) {
    acc[i].onclick = function(){
        this.classList.toggle("active");
        var panel = this.nextElementSibling;
        if (panel.style.display === "block") {
            panel.style.display = "none";
        } else {
            panel.style.display = "block";
        }
    }
}
</script>
 
</body>
</html> 
Antonio Estevez

Posts: 503
Registered: 4/12/00
Re: JavaScript errors in TCppWebBrowser
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 23, 2017 11:55 AM   in response to: Rolf Fankhauser in response to: Rolf Fankhauser
El 23/02/2017 a las 20:10, Rolf Fankhauser escribió:
Hi all,

I use a TCppWebBrowser component in my application to inform the users about news. The loaded html page is located on my hosted web server.
Now I wanted to renew my html page with some html5 code. First I tried the <details> element but it's not supported by IE (supported by Firefox and Chrome). Then I found an accordion with similar function. It uses CSS styles and JavaScript. I got an error message that the function document.getElementsByClassName is not supported.
If I load the page directly in IE11 all is fine. No problem in Firefox and Chrome.
Next, I replaced document.getElementsByClassName by a function getElementsByClassName2. Works in all browsers but not in TCppWebBrowser.
I get an error that the property "toggle" of an undefined element cannot be called.

I don't understand why it works in IE directly but not in TCppWebBrowser. I thought TCppWebBrowser uses an ActiveX element from the present OS.

I use XE5. IE11, Win7.

Thanks!
Rolf

Read this article:

https://weblog.west-wind.com/posts/2011/may/21/web-browser-control-specifying-the-ie-version

Rolf Fankhauser

Posts: 19
Registered: 9/3/01
Re: JavaScript errors in TCppWebBrowser  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 23, 2017 1:40 PM   in response to: Antonio Estevez in response to: Antonio Estevez
Thanks Antonio!

As suggested I put the meta-line in the header of the html. Now it's working. Great!
I have a little side effect: the german umlauts ä, ö, ü etc. are now wrong displayed in all browsers. I wrote the html file with a normal text editor (notepad++). I used UTF-8 encoding and wrote this also in the header.

Kind regards
Rolf
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02