0

I am a attempting to pass a URL inside a javascript onclick function but it returns missing ) error on console log, i experiment on it and found out that the URL contains special characters and sometimes spaces that escape the onclick event.

I am getting the url from a PHP script

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

and passing it on a onclick event

<a href="#" class="btn btn-primary" id="main" onclick="getContent(<?php echo $actual_link;?>);">Content 1</a>

My question is how to pass a url text inside a javascript function.

2 Answers 2

2

You need quotes around the php output in order to pass string to function:

 onclick="getContent('<?php echo $actual_link;?>');">
                  // ^^                        ^^
Sign up to request clarification or add additional context in comments.

Comments

0

Your missing a '' inside the getContent function.

onclick="getContent('<?php echo $actual_link;?>');">

Comments

Your Answer

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