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
Content 1
My question is how to pass a url text inside a javascript function.
You need quotes around the php output in order to pass string to function:
onclick="getContent('<?php echo $actual_link;?>');">
// ^^ ^^
Your missing a '' inside the getContent function.
onclick="getContent('<?php echo $actual_link;?>');">
Related
$buttons .= ' ';
onclick function how can i set my parameters with good matter
thanks
when click on button cant do the needing because there is a mistake in writing the function
parameters so cant assign my input hidden the passed parameters
because already I have been use them for datatable proposes
You can define the function and its parameters in the following way:
<button onclick="myFunction(param1, param2)">Click me</button>
In this example, myFunction is the name of the JavaScript function you want to call, param1 and param2 are the parameters you want to pass to the function.
If you need to include this button in your existing code and cannot modify the parameter names, you can use the following approach:
$buttons .= '<button onclick="myFunction(\''.htmlspecialchars($param1).'\', \''.htmlspecialchars($param2).'\')">Click me</button>';
In this example, htmlspecialchars function is used to escape special characters to prevent potential security issues, and the parameters $param1 and $param2 are enclosed in single quotes to pass them as string literals to the function.
I am working in a Laravel application. I want to concatenate a variable with a string using escape sequence (` `) in JS. A normal concatenation of strings and variable work fine using escape sequence. But it does not work and throws error when I concatenate PHP route string with a parameter. You will have an idea when you see the below code.
<script>
function changeStatus(value, applicationId){
var url = `"{{route('application.toggle.approval',['id'=> ${applicationId} ])}}"`;
console.log(url);
}
</script>
I am not being able to pass any kind of variable there. It is giving me error like below:
(2/2) ErrorException
Use of undefined constant applicationId - assumed 'applicationId' (View: C:\wamp64\www\oibn\app\Modules\Application\Views\applicationShowProtected.blade.php)
How do I pass the variable there? I can console.log the parameters passed in the function. But that value is not getting passed in concatenation. It gives error only. Please help.
I'm getting the string from controller:
var x = '<?php echo addcslashes($this->x, "'") ?>';
The parsed result is:
var x = '<script>alert(\'x\')</script>';
Error:
Uncaught SyntaxError: Invalid or unexpected token
I tried to assign the string directly from JS and it works.
The alert is coming up because the first character in the alert is a slash (the unexpected token) the second slash escapes the ' so the alert never closes either.
Not really sure what you're trying to achieve honestly with the script tags etc seems as you're already inside a script when it gets called (unless you're printing it to a page, in which case you're better off adding it to an event handler or something.)
I want to pass url parameter through html dom. This is the javascript i am using
<script>
function myFunction(url)
{
setTimeout(function(){window.location.assign(url)},4000);
}
</script>
HTML
Click here
But its not working. How can i fix this situation ? Thanks for your answers.
Use combination of single and double quotes, to pass the url as string literal. If the url is not enclosed in quotes it is considered as some variable and javascript could nod find that.
Click here
As #Adil says, you need to pass the argument as string to the function. In the way you are trying, the script is trying to recover the value of the http://stackoverflow.com variable.
what #Adil has said is true:
Click here
but if you want to pass the url as a parameter do not forget to change your function like this:
function myFunction(url)
{
setTimeout(function(){window.location.assign(encodeURIComponent(url)},4000);
}
Ok, I'm getting some data from an ajax call and need to store the value as a parameter to an onclick element via javascript.
Eg
output = "<a href = '#' onclick = 'deleteFileName('" + trim(splitString[i]) + "')'> delete File </a>";
This behaves strangely. When I inspect the element with chrome, the output looks like this
<a href="#" onclick="deleteFileName("0design mockup.xlsx')'> delete File </a>
It should obviously look like this
<a href = "#" onclick = "deleteFileName('0design mockup.xlsx')">
The parameter is a String so I need the quotes around it. This may not be the best way to do this, but why is it making the first two quotes double quotes and then the last two single quotes?
*This is a JSP server using ajax calls to gather information. The function "deleteFileName(fileNameIn)" is an ajax call. (I don't believe this is necessary information but you never know)
You can not use the same kinds of quotes for the value of onclick and for the function call. You need single quotes for one and double quotes for the other (where to put which should not matter). Otherwise you are closing the onclick attribute with the first single quote of the function call.
Example:
output = "<a href = '#' onclick = 'deleteFileName(\"" + trim(splitString[i]) + "\")'> delete File </a>";
Rather than inspecting the information, I suggest that you wget or curl the page directly (or use the browser's view-source function). These days I've found that many browsers present information subtly incorrectly in the inspector in their attempt to format everything nicely.
Removing the browser from the equation is a good first step, since that will show you exactly where the problem is occuring.