How can I mix Razor and Javascript in a VB project? - javascript

I have the following javascript/jquery snippet in a View of my vb.net based asp.net mvc project. I am trying to hit up the Browse method of my Neutrals controller.
<script type="text/javascript">
window.location.href = '/Neutrals/Browse?AreaOfLaw=' +
$('#search_area').val() + '&WebRegionID=' + $('#search_region').val();
</script>
I'd like to replace the url string with an #Url.Action, but I can't figure out how to fix the Razor syntax and that of the query parameters that I have.
I've fiddled with it for a good while and most of the time I can't even get it to compile.

Unfortunately, I don't think you can do it.
The problem is that you need those JavaScript variables - i.e. #Url.Action is executed when page is rendered on the server and you don't have JavaScript variables then.
Sure, you can construct part of url on the server, but the querystring part will need to be in JavaScript.

Use is directly as
window.location.href = '#Url.Action("action","controller")'

Related

Playframework: Call the controller's action by location.href through Javascript

Is there any way to call #controllers.app.test() by the Javascript?
It has been working fine while using
location.href = '#controllers.app.test()'
And somehow this location.href became an actual URL:
127.0.0.1:9001/admin/#controllers.app.test()
Can anyone help me out with this?
The #controllers.app.test() is calculated on the server side. Your HTML is in the view and processed by the server, that's why it becomes /admin/#controllers.app.test(). Your Javascript is just in a public folder, so it does not process.
What you can do one of the next:
Put JS file into the view (the same place where your HTML that "works fine" is placed).
Add JS object to the HTML like <script> var myAction="#controllers.app.test()"</script> and then use myAction in JS.
Use Java Script router:
3.1. https://www.playframework.com/documentation/2.6.x/ScalaJavascriptRouting
3.2. Convert a javascript variable to scala in play framework
3.3. How to update src of a img from javascript in a play framework project?

Ruby code in a JavaScript function

I'm new in the world of JavaScript, jQuery, and Ajax. I have a JavaScript function where I want to use the value of a JavaScript variable inside of embedded Ruby code. For example, below I want to use the JS p variable as an argument to the Ruby call to #r.search():
$(function (){
$('#data-select').on('change',function (){
var p = document.getElementById('data-select').value;
alert(<% j #r.search(+ p +) %>);
});
});
I want to know if i can do this? And if i can what's the syntax?
First off, I'm assuming this is Ruby on Rails (which you tagged it as). When using ERB you are telling Rails to first evaluate the embedded Ruby code to dynamically insert data that is on the server-side, and then send the file to the client over the internet where their web browser then evaluates the JavaScript.
So if this is what's in your file, and you passed a #user variable with a name method that evaluates to "John Smith":
// test.js.erb
alert("Hello <%= #user.name %>");
When that file is rendered, your server will evaluate it to this:
// test.js
alert("Hello John Smith");
And then send that over the internet to the client. Only once the client's computer has received this file will the JavaScript be evaluated. They (and the JavaScript) will therefore never see the embedded Ruby code. Therefore what you're doing cannot be done the way you have it because the Ruby code is evaluated before the JavaScript can set the p variable.
If you want to get information that resides on the server after the client has received the file and is running the JavaScript, you can use something called Ajax, which uses JavaScript to send data asynchronously back to the server. Check out this Rails guide for more details on that.
You Can't do it, because while rendering the view all ERb will be converted to HTML or relevant, so you don't have access tp the Ruby objects after rendering the view.
You can also pass the dynamic values from javascript to certain rails methods
Refer: http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/remote_function
But this rails method also will converted to ajax function after rendering.
The solution:
Try to send an ajax request and show the result in view.

Use Javascript variable in haml with datamapper

I'm having a little issue here, we are bussy with our internship and are still learning so sorry in advance for some "stupid" questions.
We have a Haml file where we make use of datamapper for our Database.
For static views based on our data, we make use of datamapper-code lines in haml.
But now I wanted to use a Javascript variable, who's located in the pageload function, to use this as a parameter with our datamapper code lines in haml.
In our javascript:
$(document).ready(function(){
var ctrpersiteid = $.cookie('sitedata');
Part of our Haml file where we want to use our variable ctrpersiteid
- Interface.all(:router_id => getRouterId(ctrpersiteid)).each do |interface|
%h3 #{interface.name}
So is there any way that I could use my Javascript variable in the haml file as parameter for our function ?
The short answer is no. Javascript is only executed in the clients browser, so there's no way to access it from your server-side haml template.

how to use "{% include 'a.html '%}" in a javascript expression on google-app-engine

this is my code:
$('.left').html("{% include 'foot.html' %}")
it will show error ,
so how to make it running.
thanks
This isn't going to do anything if your JavaScript isn't processed by webapp or Django before it gets sent to the client because the templating pseudo-language that they use will only work server-side. You'll also want to make sure you use the |safe filter or your HTML will get escaped; from what I can infer from the (very little) code you supplied this isn't the desired behavior.

How do I use RegisterClientScriptBlock to register JavaScript?

ASP.NET 2.0 provides the ClientScript.RegisterClientScriptBlock() method for registering JavaScript in an ASP.NET Page.
The issue I'm having is passing the script when it's located in another directory. Specifically, the following syntax does not work:
ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", "../dir/subdir/scriptName.js", true);
Instead of dropping the code into the page like this page says it should, it instead displays ../dir/subdir/script.js , my question is this:
Has anyone dealt with this before, and found a way to drop in the javascript in a separate file? Am I going about this the wrong way?
What you're after is:
ClientScript.RegisterClientScriptInclude(this.GetType(), "scriptName", "../dir/subdir/scriptName.js")
use: ClientScript.RegisterClientScriptInclude(key, url);
Your script value has to be a full script, so put in the following for your script value.
<script type='text/javascript' src='yourpathhere'></script>

Categories