robert_gsfame Posted April 11, 2010 Share Posted April 11, 2010 assume i have this javascript code var a="bbb" and i wish to put it inside my dynamic php code echo "<a href='page1.php&category=_____></a>"; i wish to have <a href='page1.php&category=bbb></a> where bbb was taken from javascript function is that possible??thx Quote Link to comment https://forums.phpfreaks.com/topic/198216-get-javascript-variable-and-put-it-inside-a-href/ Share on other sites More sharing options...
andrewgauger Posted April 12, 2010 Share Posted April 12, 2010 Expand the scope of the javascript function to return the entire href. <a href="javascript:getHref('page1.php&category',a)"> function getHref(x,y){ return \"+ x+"="+y+\" } you don't need this much overhead but it shows you a solid way to understand what is going on. Quote Link to comment https://forums.phpfreaks.com/topic/198216-get-javascript-variable-and-put-it-inside-a-href/#findComment-1040180 Share on other sites More sharing options...
KevinM1 Posted April 12, 2010 Share Posted April 12, 2010 Ugh, that's ugly. A much cleaner version: <!DOCTYPE html> <html> <head></head> <body> <a id="linkToChange" href=""></a> </body> <script type="text/javascript"> function generateLink(/* argument list, if necessary */) { // do whatever you need to generate the link's href and text var link = document.getElementById('linkToChange'); link.href = // something link.innerHTML = // text } </script> </html> Not a fully fleshed out example, as it depends on what you actually need to do to generate the link info, to say nothing about the number of links you need to populate. Still, much cleaner than adding the JS directly in the link's href attribute. Quote Link to comment https://forums.phpfreaks.com/topic/198216-get-javascript-variable-and-put-it-inside-a-href/#findComment-1040232 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.