I need someone to enlighten me.
I've seen this:
<script src="http://host.com/file.js?no_forms=1"></script>
What does that means? A GET parameter passed to a javascript file?
Into which conditions this can be done?
What kind of approach is this?
Any help would be appreciated.
?no_forms=1 is just a query string parameters. i will just tell the common usage of such things.
Used for to avoid caching (get new updated version of JS)
Some sort of redirection
Even some application usage ideas.
Sometimes the parameter is used simply to prevent client side caching.
It could also be that requesting file.js is actually rewritten as a dynamic call (say script.php?file=file.js&no_forms=1) that is fetching the correct file and using the extra parameter somehow.
You can use this
<script src="http://host.com/file.js" no_forms="1"></script>
And
function $_GET(q,s) {
s = s || window.location.search;
var re = new RegExp('&'+q+'=([^&]*)','i');
return (s=s.replace(/^\?/,'&').match(re)) ? s=s[1] : s='';
}
And get result
var noForms = $_GET('no_forms');
Related
I have some problem with my code, I do not understand about send multiple parameters from jquery to controller
this is my route
Route::get('/listcdix/{id}/detail/{asnumber}',
['as' => 'remindHelper', 'uses' => 'ListcdixController#detail']);
bellow is my jquery
otable.on("click", ".btnview",
function () {
var custcode = otable.rows($(this).closest("tr")).data()[0].CustCode;
var asnumber = otable.rows($(this).closest("tr")).data()[0].ASNumber;
window.location = "{{route('remindHelper',['id'=>"+custcode+",'asnumber'=>"+asnumber+"])}}";
});
but when I click this button cannot get value custcode and asnumber
this is my URL like this
http://127.0.0.1:3232/listcdix/+custcode+/detail/+asnumber+
please check my picture
thank you for attention
window.location = "{{route('remindHelper',['id'=>custcode,'asnumber'=>asnumber])}}";
try this one hope it works
You are inside a blade directive ({}), so the page is expecting you will be adding a PHP segment while inside those curly braces. You are mixing and matching between PHP and Javascript and it produces the literal +custcode+ instead of the value behind the variable. Same thing happens if you try to add it without removing the quotes - it translates it as a constant and will produce an 'undefined constant' error. I.e. you are not getting down to Javascript to get to the variable.
Use one or the other, JS or PHP. So if all Javascript, something like:
APP_URL + "/listcdix/"+ custcode +"/detail/" + asnumber;
Where APP_URL is a pre-defined variable for your url prefix. You can cheat a little on that definition if you want and use the Laravel url() method to help you. Something like:
var APP_URL = {!! json_encode(url('/')) !!};
from a blade page before the JS needs to access it.
You can use this something like that
window.location = "{{route('remindHelper',['id'=>"custcode",'asnumber'=>"asnumber"])}}";
because when use using curly braces {{}} in laravel. You do not need to concat this with ++. So that's why you can use this as simple format. i have checked this code on my end as well so this will be 100% work for you.
Thank you.
Unfortunately you can not combine js variable with php code as far as I know and you are using the route function and because your route needs argument to process, you need to pass variables inside the "route" function. as far as I know there is no way and you have to give the route in complete string way like this:
let route = "/listcdix/" + custcode + "/detail/" + asnumber
My problem is passing Map object from grails controller to JavaScript.
I have the following code inside controller
def scoreValue=new HashMap<String,String>();
scoreValue.put("0","poor");
scoreValue.put("1","good");
...
return (view:'viewname',model:[scoreValue:scoreValue]);
I have been searching for solution and have got this link pass a groovy array to javascript code. but could not help.
What I did was change the return statement to
return (view:'viewname',model:[scoreValue:scoreValue as grails.converters.JSON])
and inside gsp view I have the following code.
<g:if test="${scoreValue}">
var scoreValue=${scoreValue};
</g:if>
But what i got inside html page is the following
var scoreValue={"0":"Failure","1":"Poor"}
any help would be appreciated. thanks!
There's actually a few ways of handling GSP encoding. In addition to D. Kossatz's answer, these methods will help you out (see more at mrhaki's excellent Grails Goodness blog)
var scoreValue=${raw(scoreValue)};
var scoreValue=${scoreValue.encodeAsRaw()}
Please be aware that there is an inherent risk of cross-site scripting vulnerabilities when rendering user input unprotected on the page. So long as you know for certain that only you can set that value, and proper safe-checks to ensure it is what it is supposed to be, you should be fine.
Try:
var scoreValue= <g:applyCodec encodeAs="none">${scoreValue}</g:applyCodec>;
I need to call a controller function from javascript on my gsp.
I have read different solutions from hundreds of places but none worked.
The problem which I found closest to mine was this.
But I am not repeating the same mistake as this and thus the solution didn't help.
I have a tag like this which calls the javascript function
<g:select name="poNumber" noSelection="['':'Select PO Number']" from="${com.rerq.PurchaseOrder.list()}"
onchange="getProject(this.value)" />
And the javascript function looks like this
function getProject(poNumber){
var projectName = document.getElementById("projectName");
var newData = ${remoteFunction(controller: 'sow', action: 'getProject', params: ['poNumber':poNumber])};
}
And the function I need to call is
def getProject(String poNumber) {
String projectName = Sow.find("from Sow as s where s.poNumber=?", [poNumber])
return projectName
}
The controller function might have mistakes as I am completely new to groovy and grails. But my understanding is that the control isn't reaching here so this should not be the cause of any problem.
I am getting below exception
No signature of method: remoteFunction() is applicable for argument types: (java.util.LinkedHashMap) values: [[controller:sow, action:getProject, params:[poNumber:null]]]
I tried using remoteFunction() in g:select itself but it threw another exception which said
Attribute value quotes not closed ...
even though they were.
Any help is greatly appreciated.
To use remoteFunction with Grails 3 you need to add the ajax-tags plugin: org.grails.plugins:ajax-tags:1.0.0
Actually you can have your gsp recognize some Grails functions inside your js if the script is inside the gsp and anything you need for your js is created on the server side. In your case it seems you want to do an ajax call so you could have the following.
project.gsp (Consider that you already loaded jQuery)
<g:select name="poNumber" noSelection="['':'Select PO Number']" from="${com.impetus.rerq.PurchaseOrder.list()}"
onchange="getProject(this.value)" />
And in the same file you have
<script type="text/javascript">
function getProject(poNumber){
jQuery("#yourTarget").load("${createLink(action: 'getProject')}",{poNumber: poNUmber},function(response, status, xhr ){
if ( status == "error" ) {
alert("No data loaded.")
}
});
}
</script>
As you see a gstring in load("${}",... is used because it will be parsed in the server and at client side your actual js it will parse to load("yourcontrollerName/getProject",..... Why not code the url directly in the js? Because by using createLink() it is less likely to make reference mistakes.
EDIT
If you want to do this but using an external js file, you would need a way to pass the url, to the js, and use a simple load function. So something like this will be helpful
<g:select name="poNumber" noSelection="['':'Select PO Number']" from="${com.impetus.rerq.PurchaseOrder.list()}"
onchange="getProject(this.value, \'${createLink(action:'getProject')}\')" />
Once on the server onchange="getProject(this.value,\'${createLink(action:'getProject')}\')"would be parsed to onchange="getProject(this.value,'yourController/getProject')". Be wary that I might have messed up the ""s and ''s so verify your html output.
And you would need a js function that accepts the uri
function getProject(value, targetUri){
....
}
What you need to review is when is your function needed, on the server o in the client;if the functions are inside a gsp or not; And if not available, how could you pass data to them.
You cannot access grails's controller from javascript. I haven't tested it but this might work.
<g:createLink controller="sow" action="getProject", params="['poNumber':poNumber]"/>
Also, if you use Google Chrome's developer's tool, you will see how your javascript code is displayed. Make sure it is in right syntax.
Is there a way to do this?
<'param name = "ticket" value = "getTicket()">
getTicket() is my Javascript function. Whatever the function returns, that should be the value of the parameter tag.
I cant set it explicitly using Javascript "document.getElem...." because I want the value to be loaded at the time of page load. Or, while the parameter is being set.
For further info, I am trying to do this for Tableau Trusted Authentication.
Are you using server-side scripting?
Isn't more effectively to do what you do using the request/response ways?
Like directly using <%=ticketValue%> (ASP way) or ${ticketValue} (JSP way)?
You may use Document.Write as follows:
<'param name = "ticket" value = "<script>
document.write(getTicket());
</script>">
You can access node from JavaScript right after it was created.
You're not obligated to wait for DOMContentLoaded event. So this code will work as expected:
<script>
function getTicket() {
return 'whatever';
}
</script>
<param name="ticket" value="" class="js-ticket">
<script>
var ticket = document.querySelector('.js-ticket');
ticket.value = getTicket();
console.log(ticket);
</script>
See demo.
This approach is better than using document.write, see good explanation.
You can use jquery $(document).ready() function callback. If jquery isn't available, then you can use the equivalence of it.
$(document).ready equivalent without jQuery
Then you can access document.getElement.
Here is my question, I am using jsp script, trying to match a key word in requesting url and do something:
<script>
$url = '${pageContext.request.requestURL}';
if("${fn:contains(url, 'key')}" == true){
...
}
....
But this doest work... I am not sure where the problem is but I want it to be like when url contains this string, go in to the if condition.
Thank you
You are mixing JSP/EL and JavaScript as if they run in sync. This is wrong. JSP/EL runs in webserver and produces HTML code which get executed in webbrowser. JavaScript (JS) is part of the generated HTML code and runs in webbrowser only.
You need to do it either fully in JSP/EL, or fully in JavaScript. You can use JSP/EL to dynamically generate JS code which get later executed when the page arrives at browser. Rightclick page in browser, do View Source to see what JSP/EL has generated. You should not see any line of JSP/EL. You should only see HTML/JS code. It's exactly that JS code which get executed then.
You're using a JSP EL function to test a JS variable which isn't in the variable scope at that moment at all. This is not going to work. It can only test JSP/EL variables.
Here's how you could do it in pure JS:
<script>
var url = window.location.href;
if (url.indexOf('key') > -1) {
// ...
}
</script>
If you really insist in doing it using JSP/EL, you could do as follows:
<script>
var url = '${pageContext.request.requestURI}';
if (${fn:contains(pageContext.request.requestURI, 'key')}) {
// ...
}
</script>
This will then generate the following JS code (rightclick page in browser and View Source to see it):
<script>
var url = '/some/uri';
if (true) {
// ...
}
</script>
But this makes no sense. Whatever functional requirement you need to solve, you need to think twice about the right approach. Feel free to ask a new question about solving the concrete functional requirement the proper way.
If you want a parameter that the page was requested with, use ${param.paramName}. So in this case ${param.key}. See implicit objects in the docs. And if you just want to check it has a value try ${not empty param.key}.