!IsPostBack event from Javascript - javascript

I am using ASP.NET
I have to set the value of a variable [testVar] into Javascript on page load. only for the first time when the page load. Just like !IsPostBack event on code side.
From next postback this function of Javascript should not call. Please let me know how to implement it.

Add this line to your ASPX page:
<% if (!this.IsPostBack) %>
<% { %>
<script type="text/javascript">
document.getElementById("<%= this.timeVar.ClientID %>").value = '1/1/2010';
</script>
<% } %>

Related

Acessing Javascript variable in .aspx file showing elements conditionally

Having this .html snippet:
<body>
<div id="testDiv">
This always appears
</div>
<%
if(step==1)
{%>
html code
<% }
if (step==2)
{
%>
other html code
<% } %>
</body>
How can I access the variable "step", present in a .js file, imported using a script tag, in order to show HTML elements conditionally?
I am trying to somehow simulate the *ngIf function of Angular2, in AspX.
Thanks!
You can use it like this and create a javascript variable.
<script type="text/javascript">
var step = <%= step %>;
alert(step);
</script>
You can also use a ScriptManager
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "setValue", "var step = " + step + ";", true);
In both cases step is available in .js files.

Using Rails, how do I execute a javascript file on a specific page in the application?

I am attempting to load a javascript file onto my rails application, but only for a specific page in my application, not the homepage. How would I get my javascript file to load for a specific page is using rails.
The javascript I have now, located in programs.js in assets/javascript, looks like this:
document.addEventListener('DOMContentLoaded', (event) => {
let program = document.getElementsByClassName('program-name')
console.log(program)
})
Again, the code itself works fine. The problem is that it executes for the homepage, and not for any particular page that I want it to. How would I go about getting the code to execute for a specific page within my rails application?
Why wouldn't you add the javascript tag to the View you want it to run on?
If you wanted a bit better rendering speed, you could add a end-of-page yield to your layout and then specify your javascript in the View like this:
layout/application.html.erb
<!DOCTYPE html>
...
<%= yield(:scripts) %>
</body>
</html>
view/.../index.html.erb
<!-- regular view code here -->
...
<% content_for :scripts %>
<%= javascript_tag do %>
document.addEventListener('DOMContentLoaded', (event) => {
let program = document.getElementsByClassName('program-name')
console.log(program)
});
<% end %>
<% end %>
An other way is to do the following:
app/views/layouts/application.html.erb
<body class="<%= controller_name %> <%= action_name %>">
<!-- This will add your page's controller and action as classes, for example, "blog show" -->
Then, in your script file, you can do the following:
if($('body').is('.yourcontroller.youraction'){
// Do something
}

EJS unable to access DOM elements causing undefined

I am trying to select DOM elements via VanillaJS or jQuery. Either way they are producing undefined within my ejs file.
I load all of my libraries at the top of ejs.
In middle of the page I have a simple
<% if (somevariable < 1) { %>
<!-- do something -->
<script> document.getElementById("showorders").style.visibility = "hidden" </script>
<% } %>
Further down the page I have a table with id "showorders".
All i want to do is set the visibility of the table to hidden if the statement is true. Else continue to run down the page and do normal process.
Why is the values undefined?
The problem might occur because in your script there is a reference to an element which does not exist.
You should wait for the DOM load event.
Vanilla
<% if (somevariable < 1) { %>
<!-- do something -->
<script>
document.addEventListener("DOMContentLoaded, function() {
document.getElementById("showorders").style.visibility = "hidden";
}, false);
</script>
<% } %>
jQuery
<% if (somevariable < 1) { %>
<!-- do something -->
<script>
jQuery(function() {
jQuery("#showorders").css("visibility", "hidden");
});
</script>
<% } %>
When the browser encounters a script tag it stops parsing the page and runs the script. This is how document.write is able to add content at the current position in the page.
If you want your script to run after the DOM has been fully parsed, you should listen for the DOMContentLoaded event.
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById("showorders").style.visibility = "hidden"
});

What is the proper synatx using Mvc3 Razor for <%= %> from Mvc2

In attempting to update working code from Mvc2 to Mvc3 using the Razor engine, we found that this syntax no longer works.
<script type="text/javascript">
var initialData = <% = new JavaScriptSerializer().Serialize(Model) %>
</script>
A previous post indicated this to be "pretty trivial" but we are not finding that so. And the sample pointed to does not appear to use either json2 nor JavaScriptSerializer().
In the instant case we may choose to use an alternate method; however, it would still be valuable to know if the above line could/should work to transfer data from the #Model into a javascript variable.
#Html.Raw() is equivalent to <%= %> and #Html.Encode() is equivalent to <%: %>
<script type="text/javascript">
var initialData = #Html.Raw(new JavaScriptSerializer().Serialize(Model))
</script>

passing js function a value when using Rails button_to_function

I've got a table of what I call resources, want to show in a jquery dialog a particular record when user clicks button in a row.
in the table I'm doing:
<td><%=button_to_function 'Show','showresource1()',:class =>"ui-button ui-state-default ui-corner-all", :id => resource.id %></td>
then in javascript I want to pass jQuery dialog the resource.id and open the dialog:
<script type="text/javascript" charset="utf-8">
function showresource1()
{
jQuery("#resource_details").load( 'http://localhost:3000/resources/show/4').dialog('open');
}
</script>
I can open the dialog and load this static URL, but am stumped re passing resource.id from the button_to_function down to the js function.
is button_to_function the right way to go and if so, how do I get URL defined in js?
It should be like code below
<% for res_id in ids %>
<%=button_to_function "Show", "show_resource('#{res_id}')" %>
<% end %>
<script type="text/javascript" charset="utf-8">
function show_resource(res_id)
{
jQuery("#resource_details").load( 'http://localhost:3000/resources/show/' + res_id).dialog('open');
}
</script>

Categories

Resources