rails code within javascript - javascript

I am trying to use some rails code withing a javascript and need to have that rails code be dynamically changed. Here's the line of code:
$(this).replaceWith("<%= escape_javascript(render(:partial => 'shared/products')) %>");
The 'shared/products' is the part I want to change based off information passed earlier in the javascript. How do I insert a value from javascript so that instead of 'shared/products' the products portion can be a variable? Hope this makes sense. I'm not the most experienced jQuery/javascript programmer, so any help is very much appreciated. Thanks in advance!

No, you cannot do that way. rails codes may run on page loads, but when you write those with javascript, those are just texts. If you really need to run server side scripts, use AJAX.

Related

how can i use PHP functions (Gutenberg WordPress) inside JavaScript

sorry for asking a very basic question...
i am new at WordPress/Gutenberg, and I am not familiar with PHP. trying to write a custom block with javascript at Gutenberg, but I found most of the functions available only work at PHP. how I can use these functions in javascript ..
like .. get_post_meta() , or get_the_tags()
thank you
Yes, PHP functions can be used inside Gutenberg blocks by creating a dynamic block which calls a PHP function to render the content. This enables use of all the available PHP & WordPress functions like get_post_meta() etc.
The Developer documentation has an example block code that shows how this is implemented and is a great place to get started. Also, the <ServerSideRender> is a useful component that enables rendering of live PHP inside the block editor.
Reviewing the source code of an existing core block (eg. latest posts) that uses PHP to render may also help you get started with building your own block.
There is a workaround since PHP outputs text you can use it to write Javascript code that will be executed on the page load or triggered by an event. Just don't forget to surround your js code with script tags

How to access environment variable from rails on your javascript?

I am trying to set up a key system in the application I'm building. I have a code that has this logic:
$('.key-test-button').on('click', function(){
if ($('.key-test-input').val() === "MYKEYHERE"){
$('#hidden-div-instructor').show();
}
});
And My Idea was to replace "MYKEYHERE" for a hidden value on my code that I would store as a environment variable. I know how to do that for APIs for example, inside rails, but Im not familiar how I could access this variable inside my javascript/jquery script. I would love if someone could give me a insight. Thanks you.
As #davidhoelzer mentioned, you can't do this with pure js (as far as I know)... you could, however, change the .js file to .js.erb and use ruby to access/manipulate your environment variable that way.

How do I extract data from a website using javascript.

Hi complete newbie here so bear with me. Seems like a simple job but I can't seem to find an easy way to do this.
So I need to extract a particular text from a webpage "www.example.com/index.php". I know that the text would be available in p tag with certain id. How do I extract this data out using javascript?
What I'm trying currently is that I have my javascript file (trying.js) on my computer with the following code:
$(document).ready(function () {
$.get("www.example.com/index.php", function(data) {
console.log(data)
}) ;
});
and a html that runs the javascript file.
When I open this html page with firefox it doesn't show me anything in console. How do I get the website's data? Am I on the correct track here? Is there a better way to do this?
What you're looking for is a page scraper. Javascript can't pull it off because it can only gather data from the domain you're on.
You could build it in Ruby, for example, and use one of the many existing gems for this sort of task, like https://github.com/assaf/scrapi or http://nokogiri.org/
Please take a look at Can Javascript read the source of any web page?
There are multiple ways discussed. Hope it helps you.

Rails 3 and jQuery- how to transmit ruby variable to javascript

I would like to ask you - I have javascript code and I need into this part of code transmit variable with data from database. This variable is in controller. How can I do it?
I tried something like (in *.js):
$("#div").append('<%= escape_javascript(#test) %>hhhhhh');
or
$("#div").append('<%= #test %>hhhhhh');
But the codes above not works... Could you help me, please, how to do?
Thank you, Manny
If you are rendering a response to AJAX-call, that code should go in your_action.js.erb file. Otherwise, if it's just inline JavaScript, place it in your_action.html.erb under app/views/your_controller directory.
Correct me if I understood you wrong.

What is the most efficient and robust way to program ASP.NET with javascript?

I wanted to know what is the best approach to program ASP.NET with JS?
So far, I've been trying to code with the Codebehind attributes.add("onclick," ....");
is there a better more efficient way to use JS with ASP.NET?
thanks
writing all your function in a separate js file is far more flexible that what you are doing now. The problem might be getting the control ID's in your js file.
Well there are many methods.
Hardcoding
Just view the source and figure out the ID of the control which you want to use.
this is easy but if you you may want to change the ID of the control at some point of time the respective functions will not work
Something like This
Declare a variable with all the the ClientIDs in the aspx file use it in the js file
var MyControlIDs = {"Textbox1":"<%= Textbox1.ClientID %>","Label1":"<%= Label1.ClentID %>"};
and in your js file var mycontrol= window.MyControlIDs
and use it wherever you want
var textbox = document.getElementById(mycontrol.TextBox1);
Many WebControls have an OnClientClick property, which is equivalent of what you're doing. Also you can always set it in your markup for controls that don't have the property.

Categories

Resources