Passing string variable in alert box - javascript

I have below script where im passing scriptlet variable to javascript function. But there is no alert box displaying.. Please let me what im missing here..
<script language="JavaScript">
window.onload = function(){
<%
String res = request.getParameter("Message");
%>
var Value = "<%=res%>";
alert(Value);
document.Form.submit();
};
</script>
Thanks in advance

try this....
<script type="text/javascript">
window.onload = function(){
<%
String res = request.getParameter("Message");
%>
var Value = <%=res%>;
windows.alert(Value);
document.Form.submit();
};
</script>

What you need to use is <%= ... %>. Try using this:
<script type="text/javascript">
window.onload = function(){
<%
String res = request.getParameter("Message");
%>
var Value = <%=res%>;
alert(Value);
document.Form.submit();
};
</script>

#John Why you creating scriptlet tag inside javascript. Instead try using this,
<%
String res = request.getParameter("Message");
%>
<script type="text/javaScript">
window.onload = function(){
var Value = "<%=res%>";
alert(Value);
document.Form.submit();
};
</script>
else try using jquery plugin for your purpose,
<%
String res = request.getParameter("Message");
%>
<script type="text/javaScript">
$(document).ready(function() {
var Value = "<%=res%>";
alert(Value);
$('form#myForm').submit();
});
</script>

Related

Get access of DOM element on other function by this in jquery

I have a button
<button type="button" data-id="5" class="get_attr" >Click Me</button>
and i want to get the data-id value then i write the code
<script type="text/javascript">
$(".get_attr").on("click",function(){
var id = $(this).data("id");
console.log("id = "+id); //id = 5
});
</script>
Suppose I want this value, or do any functionality on this button from another function if i call function from this code like and I want like this:
<script type="text/javascript">
$(".get_attr").on("click",function(){
updateValue(this);
});
function updateValue(this){ // it will take it as a parameter
var id = $(this).data("id");
console.log("id = "+id); //id = 5
}
</script>
You can not use 'this' becasue it is Javascript keyword
<script type="text/javascript">
$(".get_attr").on("click",function(){
updateValue(this);
});
function updateValue(abc){ // it will take it as a parameter
var id = $(abc).data("id");
console.log("id = "+id); //id = 5
}
Now its work fine...
Being this is a reserved word, change the parameter name from this to some other like in updateValue(thatObj).
$(".get_attr").on("click",function(){
updateValue(this);
});
function updateValue(thatObj){
var id = $(thatObj).data("id");
console.log("id = "+id);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" data-id="5" class="get_attr" >Click Me</button>
You cannot use a reserved keyword in your function definition. It should work with simply using another name in the definition.
<script type="text/javascript">
$(".get_attr").on("click",function(){
updateValue(this);
});
function updateValue(originalObject){ // it will take it as a parameter
var id = $(this).data("id");
console.log("id = "+id); //id = 5
}
</script>
Try this
$(".get_attr").on("click",function(){
var id = $(this).data("id");
console.log("id = "+id); //id = 5
});
$(".get_attr").on("click",function(){
updateValue(this);
});
function updateValue(x){ // it will take it as a parameter
var id = $(x).data("id");
console.log("id = "+id); //id = 5
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<button type="button" data-id="5" class="get_attr" >Click Me</button>

Refactoring Javascript code from Aspx file

I am refactoring the web project,where java script and css code is written inside the .aspx file.Now I am removing java script and css code from the aspx files and maintaining that the code in respective css and js files with their reference in aspx files.
It is straight forward for me if java script code is written in one place and inside the funtion.Here in the example code not all java script code is inside the function and some of the script are inside the div block.I am not able to put these script in separate file.Moreover I am new bee in Java script and web development please help me on this.
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
-------code-----------
</script>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
<div class="formColWhole" style="width: 935px;">
<script type="text/javascript">
document.body.style.cursor = 'default';
document.forms[0].action = '<% = ViewData["RegisterPageUrl"].ToString() %>';
function IndicateProcessing() {
if (typeof (Page_ClientValidate) == 'function') {
if (Page_ClientValidate() == false) { return false; }
}
var cancelButton = document.getElementById("<% = buttonCancel.ClientID %>");
if (cancelButton != null) {
cancelButton.disabled = true;
}
document.body.style.cursor = 'wait';
return true;
}
function submitForm() {
IndicateProcessing();
// _gaq.push(['_trackEvent', 'Register', 'Register submit Click', 'Register submit']);
// avoid duplicate submission
var button = $("#buttonSend");
button.attr('disabled', true).html('');
document.forms[0].submit();
}
</script>
</div>
<div class="formColWhole" style="width: 100%;">
--------code----
</div>
<script type="text/javascript">
var selectCountryText = document.getElementById("<% = selectCountry.ClientID %>").value;
var regions = document.getElementById("region");
regions.firstChild.text = selectCountryText;
</script>
</asp:Content>

jQuery cant get value from textbox

I am trying to get a value from a textbox so that that I can rotate an Image using jQuery
<script src="Scripts/jQueryRotate.2.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var angle = $("input:TextBox1").val();
$("#needle").rotate(angle);
alert(angle);
});
</script>
And I'm populating the textbox as follows
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
WeatherLibrary.WeatherData wLib = new WeatherLibrary.WeatherData();
double dataLatest;
string sensorName;
sensorName = "umtAdjWinDir";
double dir = wLib.GetLatestData(sensorName).Value;
dir = Math.Round(dir, 0);
TextBox1.Text = dir.ToString();
}
</script>
TextBox1 is the the id of the textbox
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True"></asp:TextBox>
The alert I have given says undefined and the image never gets rotated.
Try
var angle = $("#TextBox1").val();
OR
var angle = $("#<%= TextBox1.ClientID %>").val();
If TextBox1 is the id of the input element you can use var angle = $("#TextBox1").val(); to get the value form the textbox
Try using :
var angle = $('#<%=TextBox1.ClientID%>').val();
try
var angle = $("input#TextBox1").val();
assuming you have an input with id="TextBox1"
I think not sure it may be a problem of master page or user control.
<script src="Scripts/jQueryRotate.2.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var angle = $("#TextBox1").val();
$("#needle").rotate(angle);
alert(angle);
});
</script>
Supposed to be
var angle = $("input[id*='TextBox1']").val(); // For ID
var angle = $("input.TextBox1").val(); // For class
var angle = $("[id*='TextBox1']").val(); // For ID this is sufficient
Need to use attribute ends with $ or attribute contains selector * if you are using ASP.NET controls with runat="server" attribute
$("#TextBox1").val() this code will help u out to get the textbox value..

Why am I getting a “hover is not defined” error in my JavaScript?

What’s going on? It says that “hover is not defined”, but in my jsFiddle, the code works fine:
click
<div id="1"></div>
<script type="text/javscript">
function hover(x) {
var id = x;
var hoverBubble = document.getElementById(id);
hoverBubble.innerHTML = "hello";
}
</script>
<script type="text/javscript">
That is misspelled. It should be javascript
<script type="text/javascript">

Extract script tags from a HTML string

Or any other tags :)
for eg.
<head>
<title>page...</title>
<script> var a = 'abc'; </script>
<script src="foo.js" type="text/javascript"></script>
</head>
<body>
...
<script src="foo2.js"></script>
</body>
(this string is a response from a ajax call)
I want to get a array with 3 strings:
<script> var a = 'abc'; </script>
<script src="foo.js" type="text/javascript"></script>
<script src="foo2.js"></script>
How can I do that?
Define: outerHTML function (taken from here)
jQuery.fn.outerHTML = function(s) {
return (s) ? this.before(s).remove() : jQuery("<p>").append(this.eq(0).clone()).html();
}
Then assume your response is stored in data you can do:
$(data).filter("script").each( function(e) {
// do something with $(e).outerHTML()
} );
Use a regular expression with the pattern <script[^<]*</script>.
You can try something like:
function getScriptsAsText() {
var div = document.createElement('div');
var scripts = [];
var scriptNodes = document.getElementsByTagName('script');
for (var i=0, iLen=scriptNodes.length; i<iLen; i++) {
div.appendChild(scriptNodes[i].cloneNode(true));
scripts.push(div.innerHTML);
div.removeChild(div.firstChild);
}
return scripts;
}
It returns a array of the current script elements as text, including their start and end tags.
You might also try outerHTML, but it's not that widely supported.

Categories

Resources