2

Solution

This was not the real issue. The issue was that one of the javascript didn't load.

I asked a new question about it here: Can't call functions from first included javascript file from the second included js file

My original question

When I view my application in a browser (before I build/compile it with Cordova), everything works fine. But after i build it with Cordova, $("#content").html("test"); doesn't work anymore on android 4.2.2. however, it does work on android 6.0.0. alert("test");.

First I though that jQuery isn't working... But then I tried this:

$("body").click(function() {
    alert("test");
});

and it worked.

Any ideas why the .html() method is not working?

UPDATE

This is how the element with the id "content" looks like:

<div id="content">
</div>

I tried to add some content like this:

$('#content').html(`<span>test1</span>`);

On all android versions i use Google Chrome as my browser.

UPDATE #2

I tried the html() method inside and outside of

$(document).ready(function(){});
Community
  • 1
  • 1
tedi
  • 6,350
  • 5
  • 52
  • 67
  • 1
    i think the problem here is with the browser versions. Android 4.2.2 uses the stock browser while android 6 uses chrome. Unless you have crosswalk ofcourse. I would try the same code wraped within a setTimeout and see how it goes. `setTimeout(function() {$('#content').html("test1")}, 200)` – Sarantis Tofas Dec 07 '16 at 20:50
  • I use the chrome browser on 4.2.2, will add this info to my question. – tedi Dec 07 '16 at 20:55
  • @Akis i tried setTimeout with 200 and 1000, didn't work. I started a new question here: http://stackoverflow.com/questions/41116344/cant-call-functions-from-first-included-javascript-file-from-the-second-include – tedi Dec 13 '16 at 08:15

2 Answers2

3

Start with: Make sure the <div id="content"> is loaded to DOM before you 'set text' script executes, could utilize onload() to ensure. Probably checked this, but a heads up for others.

Next, I wonder if the issue is with the .html() method of JQuery. Documentation states "This method uses the browser's innerHTML property."

Check innerHTML() alone via:

document.getElementById("content").innerHTML = "test";

I know there are certain limitation when utilizing innerHTML() in vanilla JS, for instance issues with <script>, so if JQuery's .html() utilizes it, it may be an issue somehow.

If you can, try using vanilla JS to set the #content <div> via:

document.getElementById("content").textContent = "test";

This will allow you eliminate .html() and it's use of .innerHTML() truly isn't to blame.

Edit: Here is JQuery's .html() method, the true issue may lie with how it handles the setting. It attempts to use innerHTML(), if that fails somehow, it then defaults to append().

See below:

function (value) {
    return access(this, function (value) {
        var elem = this[0] || {},
            i = 0,
            l = this.length;

        if (value === undefined && elem.nodeType === 1) {
            return elem.innerHTML;
        }

        // See if we can take a shortcut and just use innerHTML
        if (typeof value === "string" && !rnoInnerhtml.test(value) && !wrapMap[(rtagName.exec(value) || ["", ""])[1].toLowerCase()]) {
            value = value.replace(rxhtmlTag, "<$1></$2>");
            try {
                for (; i < l; i++) {
                    elem = this[i] || {};
                    // Remove element nodes and prevent memory leaks
                    if (elem.nodeType === 1) {
                        jQuery.cleanData(getAll(elem, false));
                        elem.innerHTML = value;
                    }
                }
                elem = 0;
                // If using innerHTML throws an exception, use the fallback method
            } catch(e) {}
        }
        if (elem) {
            this.empty().append(value);
        }
    },
    null, value, arguments.length);
}

Furthermore, here is the source for the fallback default called, append(), if the innerHTML() in the html() method fails:

function () {
    return this.domManip(arguments, function (elem) {
        if (this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9) {
            var target = manipulationTarget(this, elem);
            target.appendChild(elem);
        }
    });
}

You can find the methods in JQuery by searching with Ctrl + F and searching when viewing the source... for instance html:, with the colon. See picture below:

JQuery Search for function/method in source

Notice the search bar on the bottom.

NSTuttle
  • 3,237
  • 2
  • 17
  • 26
  • Thanks! `document.getElementById("content").textContent` works. The`.html()` method is implemented in a way which doesn't fancy Chrome on old Android versions i guess. Any idea why that might be? I will check out the documentation for `innerHTML()`. Can it be, that Chrome on Android 6.0 uses a different verion of JavaScript/ECMAScript then Chrome on Android verion 4.2.2? – tedi Dec 08 '16 at 07:26
  • 1
    The only problem with the `.textContent` property is, that i can't insert HTML tags which i need. Any other solutions maybe? – tedi Dec 08 '16 at 07:54
  • 1
    Solved it by using the JavaScript `.innerHTML` property. No idea why it doesn't work with the jQuery `.html()` method. – tedi Dec 08 '16 at 08:06
  • This could be due to how `.html()` handles setting the text, I dug up the source for the function and placed above for reference. If you were feeling spicy you could run through each check and see where it fails. – NSTuttle Dec 08 '16 at 16:00
  • Thanks a lot. This really helps me to understand where the issue lies. Is this the whole code for the jQuery `.html()` method? And could you tell me in which file of i can find this (when i download the whole jQuery "library" from their website)? – tedi Dec 08 '16 at 21:06
  • 2
    Of course. If you are viewing JQuery as the one file, from [this download page](https://jquery.com/download/): Open the document and Ctrl + F for find, type `html:` and it should be found. Functions are defined as `html: function( ) {` and so on. – NSTuttle Dec 08 '16 at 21:14
  • 1
    I can't find on which line the function is located here: https://code.jquery.com/jquery-3.1.1.js – tedi Dec 08 '16 at 21:19
  • 1
    Never mind... Found it, just searched for "`html: function( value )`". Thanks! – tedi Dec 08 '16 at 21:20
0

I think the reason why your .html() is not working is because it might not be binded to an action/element whereas alert() in itself is a message box or way of conveying information to the user. Let us see using example:

  1. Alert():

    $("body").click(function() {
        alert("test");
    });
    
  2. html():

    $("body").click(function(){
         $("h1").html("test");
      });
    

here as you can see clearly, button triggers an action of click which would then change the content of "h1" but alert does not requires any such biding to an element or so.......

Hope this is helpful :)

Nikita Dhiman
  • 182
  • 3
  • 12
  • So what's the solution? – tedi Dec 05 '16 at 11:25
  • check your jquery thing again because alert() does not requires jquery to work its a simple js functionality which is enough for alert to work. html() on other hand necessarily requires jquery. – Nikita Dhiman Dec 05 '16 at 11:27
  • But i already tried to call `alert()` within `$("body").click(function() {});`, so if the alert method gets triggered here, that means that jquery is working right? As `$("body").click();` is jQuery. – tedi Dec 05 '16 at 11:31
  • or just simply add html() to some element as shown in example...first try for some tag and then for an element with id – Nikita Dhiman Dec 05 '16 at 11:32
  • post the code containing that element with content as id as you were trying and lets find out what's the problem – Nikita Dhiman Dec 05 '16 at 11:33
  • I did. The div with the id "content" is inside the `` tag/element. – tedi Dec 05 '16 at 12:30
  • try this..........instead of ("body").click() ...make a button with id say buttonCheck and then try ("#buttonCheck").click(){$("h1").html("test");} – Nikita Dhiman Dec 05 '16 at 13:08
  • It's still the same... It works on android 6.0, but not on 4.2.2 i tried on both. – tedi Dec 05 '16 at 13:29
  • I tried this: `$("#test").click(function(){$("#content").html("test");});` with a button: `` and it still didn't work. – tedi Dec 05 '16 at 13:36