0

I'm using an external javascript file (eg:myjs.js) with function setval() to set the innerText property of a HTML lable.

I have PHP page with <lable id="abc"></lable> and I have included myjs.js in <head> section. myjs.js is included in <head> section of one more PHP file and I'm calling setval() from this file, so when it runs it must set the innerText of lable in the first PHP file.

Is it possible to achieve? Any suggestions are greatly appreciated. I hope I made question clear.

Thanks

4
  • There is no such thing a a "PHP page", it's all HTML on the client. Forget about what PHP is doing, think about the client end on the browser. Pretend it's just an HTML page and you'll find the answer. Commented May 11, 2012 at 15:11
  • @F.Calderan Hm, maybe you should not have fixed lable, it might cause problems if the OP really uses that. Commented May 11, 2012 at 15:13
  • 1
    @bažmegakapa if the problem is only caused by a mistyping the whole discussion has no real problem to be answered :) @user725719 could you confirm if you're using <label> ? Commented May 11, 2012 at 15:17
  • 1
    As this has nothing to do with either PHP or ajax, I've removed those tags. Commented May 11, 2012 at 15:22

1 Answer 1

1

Ideally, move your include of myjs.js to the bottom of the page (just before or after the closing </body> tag). Then it's just:

document.getElementById("abc").innerText = /* ...the text */;

Live example | source

If you can't move the include for some reason, you can put the above code in a function, e.g.:

function updateTheLabel() {
    document.getElementById("abc").innerText = /* ...the text */;
}

...and then put this script just after the label:

<label id="abc"></label>
<script>
updateTheLabel();
</script>

Live example | source

If you don't want to do that (and it's a bit ugly, mixing script tags into your markup like that), you could wait for some kind of event that occurs when the page is ready. Search for "javascript dom ready event" and you'll see a lot of discussion of this, and lots of solutions. If you're using a library jQuery, Prototype, or any of several others, odds are high (but not 100%) that the library will have something for you. You probably don't want to wait for the window load event, because that fires very late in the page load process (after all external resources — images and such — are loaded).

(Can't do a live example of the above without knowing what library, if any, you're using; but again, there are dozens of "dom ready" solutions out there.)

Or I suppose worst case, you could poll, using this in myjs.js:

function updateTheLabel() {
    var elm = document.getElementById("abc");
    if (elm) {
        elm.innerText = /* ...the text */;
    }
    else {
        setTimeout(updateTheLabel, 50);
    }
}
setTimeout(updateTheLabel, 0);

Live example | source

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.