Jump to content

Get a variable value from a javascript function


grs5211

Recommended Posts

I have a function that gets the person who last modified a document. In simple terms here is the function.

function extractModifiedBy()

{

var ModCode = 'It was John Doe that was here' ;

 

//alert("Extract code=" + ModCode.substr(8,8));

return ModCode.substr(8,8);

    }

I wish to diaplay this value in a table row.

 

When I run this code I get no result.

<td colspan="1">

    <?php echo "Last Modified by : " . "<SCRIPT LANGUAGE='javascript'>extractModifiedBy();</SCRIPT>" ?>

</td>

Link to comment
Share on other sites

Does it alert correctly?? try calling the function from a button thats not brought it by PHP and see if the js is working.

 

other than that all i can see is invaild markup

 

don't mix your lower case and up case tag names always stick to lower case and escape your " rather then use ' 

 

example

<?php echo "Last Modified by : " . "<script language=\"javascript\">extractModifiedBy();</script>" ?>

//also try

"<script type=\"text/javascript\"> // seems to work better for me!

Link to comment
Share on other sites

Exactly. J.Daniels was only have right. You can pass php variables to javascript, but you cant pass javascript variables to php. And in this case, the problem wasn't related to that anyways, in that this really wasnt a php issue at all. The problem was that your function returned a value, but didn't do anything with it - i.e. print it to the page. gijew gave you a solution that will work (not one I would use, but it will still work).

Link to comment
Share on other sites

Thanks for all the responses. I know this method is a bit unorthodox, but it would really solve the issue I have in this contect.

Well..tried all the above, but the function was never fired. I have an alert in the function, but I don't see it.

Testing the function with a button works just fine.

Is there any reason why <?php echo "Last Modified by : " . "<script language=\"javascript\">extractmodifiedby();</script>"; ?> will not fire the function.

This code is placed directly after the <form> tag.

 

Link to comment
Share on other sites

</html>
<head>
<script language=javascript type='text/javascript'>
function extractModifiedBy() {
var ModCode = 'It was John Doe that was here' ;

//alert("Extract code=" + ModCode.substr(8,);
return ModCode.substr(8,;
}

function setMod() {
var lastMod = document.getElementById("lastMod");
var modBy = extractModifiedBy();
lastMod.innerHTML = modBy;
}
</script>

</head>
<body onload="setMod()">

Last Modified by : <div id="lastMod"></div>

</body>
</html>
[code]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.