Trying to access an external JavaScript function from within a PHP script -


rewording previous php/javascript question regarding access external javascript function within php script.

i trying access javascript function myjsfnct part of myjs.js javascript file myphp.php php script.

i using windows7, internet explorer 11.0.9600, , xampp-v3.2.1 php-5.5.9.

my web root directory c:\apache-php-mysql\htdocs.

myphp.php , myjs.js in c:\apache-php-mysql\htdocs\my-php-scripts.

here skeleton of myphp.php, , myjs.js contains javascript function myjsfnct.

note, myphp.php , myjs.js parts of large php/javascript project contains several hundreds lines of code. narrowed problem down code not work. since skeleton code part of larger project, can't replace php code html code, or replace link button. need find out why php code can't access javascript function.

the actual project taken php , mysql dynamic web sites, 4th edition, larry ullman, chapter 11, "php , javascript".

myphp.php

<!doctype html>  <html lang ="en"> <head>  <title>page-php-js</title> <meta http   equiv="content-type"   content ="text/html;   charset=utf-8" />  <script type="text/javascript"  charset="utf-8"   src="myjs.js"> </script> </head> <body>  <?php  print  "<a href=\"javascript:myjsfnct()\">click</a> call js function (myjsfnct)"; ?>  </body> </html> 

myjs.js

function myjsfnct() {      /* code lets me know        javascript function has been called */ } 

related question, there way determine javascript function has been called?

when using debugger (f12 developer tools), php page looks ok 0 errors, warnings, or messages, when click on javascript link following error: 'myjsfnct' undefined.

thanks time.

jerry - wa0h

this isn't answer hoping for, but: you're doing things wrong, , things @ least closer right.

step 1 know php runs, javascript runs, , html should like.

based on you've shown, you're not using actual php code, don't need kind of <?php ... ?> tags , string escaping. anywhere.

what should have plain index.php file intents , purposes, html:

<!doctype html>  <html lang ="en">   <head>      <title>page-php-js</title>     <meta charset="utf-8">     <script src="myjs.js"></script>   </head>   <body>      <button onclick="myjsfnct()">click</button>   </body> </html> 

there no php code in here, because you're not using php. requesting index.php straight pass through. note proper html5, unlike source: <meta> has charset attribute, format used html 4.01. fine if said using html 4.01, <!doctype html> instruction explicitly tells browsers "this html5!". further, meta not /> closed. <script> elements don't need type javascript (neither <style> elements css).

you used <a> if it's button - there <button> element use instead. use <a> if actually link out resource (either url or #fragmentidentifier). if don't want button, that's irrelevant: it's semantic role. if can click it, cause js something, it's <button> , use css make need (including plain text. button styling creative background/foreground/border coloring browsers , overridable).

now, php going nothing here. runs on server, , generates content on request someone's browser. gets transfered - php dies. literally, process killed , server waits more new request. if php files, start php again, again die once it's generated data request.

so page sent no longer under php control: lives in browser, "client-side", on computer of whoever asked url. can you, on same computer that's running php, in terms of "what controls data" doesn't matter: php died, browser go data. we're in html+javascript-only territory.

so, fixing javascript: do not use document.write. it's ancient, obsolete js call absolutely doesn't think does. if thought wrote data page, can yell @ whoever told use it: not does. pump data active data stream, , if there isn't one, makes new data stream browser work with. means if call any time after page has loaded, wipe entire page clean, , starts new write stream browser decode. page gone. it's terrible function , should never, ever, use it.

(there times when want use it. times extremely rare, , low level , related workin on browser file system or stream interpreter. , typically it's bad idea)

what want dom manipulation. instead of myjs.js this:

function myjsfnct() {    document.write ("now inside js function (myjsfnct)</br></br>"); }  

you want this:

function myjsfnct() {    var p = document.createelement("p");   p.textcontent = "this paragraph written javascript function";   document.body.appendchild(p); } 

this builds new paragraph proper way, assigns text content (don't tempted use innerhtml if there no html. , if there html, you're unwittingly baking in potential exploit right there). paragraph added end of page. done.

finally, don't use <br> elements. holdover when didn't have useful css, , needed special elements add vertical spacing. css now, , <br> literlly when need semantic line break. not visually, when machine reader should see line has terminated , resumed next line whatever reason. under normal circumstances, there no such reasons.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -