Webpage not found javascript error - javascript

I am very new to javascript and was practicing when i came accross this error where my web browser can't find the html document when the location variable is inside the <script</script tags
<script>
<!--
var location = "Syrdsase va";
var name = "bob sixlet";
var age = 14;
document.write(name + ", " + age + location);
//-->
</script>

juste don't use location as a variable it will redirect you to a new page. change your variale's name and it will work
some reserved variables in js
http://www.quackit.com/javascript/javascript_reserved_words.cfm

There is a location object which when assigned to, changes the location of the page:
location = 'http://www.google.com'; // goes to Google homepage
Usually, to avoid confusion, it is usually referred to using window.location, but since window is simply the global object, reassigning location will make the page go to the URL that is the value of the string.
To solve the issue, simply rename the variable:
var myLocation = "Syrdsase va";
var name = "bob sixlet";
var age = 14;
document.write(name + ", " + age + myLocation);

Related

How to add an additional value to a input box

I'm trying to edit a custom php/JS script input box so a custom suffix is sent with a user inputted value. But so far I haven't managed to succeed.
I need to add ",UK" onto the address variable.
I have tried this
var address = search_form['address'].value + ",UK",
But no luck.
getCurrentLocations: function()
{
var self = this,
search_form = document.forms[self.opts.search_form_name],
address = search_form['address'].value,
radius = search_form['radius'].value;
I'm just not seeing the address var attaching ",UK"
Maybe this is what you need?
search_form['address'].value = search_form['address'].value + ", UK";
You need to store old value before appending, then you can create new value in new variable like:
var old_address = search_form['address'].value;
var new_address = old_address + ", UK";
You can also append by using addition assignment operator += like:
var address = search_form['address'].value;
address += ", UK";
Working Example 1:
var address = "Mike Address";
address += ", UK ";
console.log(address);
Working Example 2:
var address = "Mike Address";
var new_address = address+", UK ";
console.log(new_address);

Define session variable in c# get it from javascript

I'm saving string that represents the URL in a session variable from my code behind like this:
String mois = Request.QueryString["mois"].ToString();
String m = mois;
String moisnom = Request.QueryString["moisnom"].ToString();
String annee = Request.QueryString["annee"].ToString();
String dt = Request.QueryString["date"].ToString();
String user = Request.QueryString["user"].ToString();
String where = "jour.aspx?mois=" + mois + "&moisnom=" + moisnom + "&annee=" + annee + "&date=" + dt + "&user=" + user + "&cp=all" + "&usl=" + Request.QueryString["usl"].ToString();
Session["togo"] = where;
And then I try to get it like this in JavaScript like this:
var togo = '<%=Session["togo"]%>';
// i also tried this var togo ='#Session["togo"]';
var newPage = togo; // this should contain a string with the url to go to
But when I use it it uses it as a string here is what my URL looks like:
http://localhost:50311/<%=Session["togo"]%>
or
http://localhost:50311/#Session["togo"]
How else can I access the session variable or what am I doing wrong please?
EDIT:
like you suggested i already tried using the hidden field like this
yes i tried that but then i had this problem here is the definition of the hidden field
<input type="hidden" value="aa" id="myHiddenVar" runat="server"/>
then i tried giving it the value i need on click
String where = "jour.aspx?mois=" + mois + "&moisnom=" + moisnom + "&annee=" + annee + "&date=" + dt + "&user=" + user + "&cp=all" + "&usl=" + Request.QueryString["usl"].ToString();
myHiddenVar.Value= where;
and this is how i tried getting it from the js file
var togo = $('#myHiddenVar').val();
var newPage = togo;
but it takes the default value meaning "aa" as in value="aa" i gues cause the script is executed before the assignment of the variable any way how to reverse that order ?
After Session["togo"] = where;
save this Session["togo"] in hidden variable
hiddenVariable= Session["togo"];
Now in JS access that hiddenvariable:
suppose ID of hiddenvariable is "hdnxyz"
var togo = $('#hdnxyz').val();
var newPage = togo;
first of all session resides on server!!!!!!
If it is in different js file than you cant access it <%xyz%> i.e scriplet tags only work on aspx page...
so there is no way to access the session variable on client side..
instead assign your sessio9n value to a hidden variable and then access it using javascript
Write Session element in a asp:HiddenField and then read from it with your js code.

Removing parameter values of a url in the next page using javascript only

I need to remove the values from the url after the ? in the next page the moment i click from my first page. I tried a lot of coding but could not get to a rite path. Need help.
The strings ex- Name, JobTitle and Date are dynamically generated values for ref.
Below are the links associated with the code:
Required url
file:///C:/Users/varun.singh/Desktop/www%20updated%2027.8.2015%20Old/www/Candidates/newOne.html?
Resultant url:
file:///C:/Users/varun.singh/Desktop/www%20updated%2027.8.2015%20Old/www/Candidates/newOne.html?Name=Name%201&JobTitle=Title%201&Date=Entered%20Date%201
listItem.onclick = function(){
var elementData=listData[this.id];
var stringParameter= "Name=" + elementData.name +"&JobTitle="+elementData.job_title+"&Date="+ elementData.entered_date;
//window.location.href = window.location.href.replace("ListCandidateNew", "newOne") + "?" + stringParameter;
window.location.href="file:///C:/Users/varun.singh/Desktop/www%20updated%2027.8.2015%20Old/www/Candidates/newOne.html?"
+ stringParameter;
}
This should work:
var url = file:///C:/Users/varun.singh/Desktop/www%20updated%2027.8.2015%20Old/www/Candidates/newOne.html?Name=Name%201&JobTitle=Title%201&Date=Entered%20Date%201
var index = url.lastIndexOf("?");
url = url.slice(0, index+1); // index+1 so that "?" is included
Thanks everond for trying and attempting to answer my problem. Well, i have found the solution using window.sessionStorage as i wanted by keeping the string parameter alive to pass the values. Here is the full code:
I have two pages for passing the value from one to another: ListCandidateNew.html and newOne.html
ListCandidateNew.html
listItem.onclick = function()
{
var elementData=listData[this.id];
var stringParameter= "Name=" + elementData.name +"&JobTitle="+elementData.job_title+"&Date="+ elementData.entered_date;
window.sessionStorage['Name'] = elementData.name;
window.sessionStorage['JobTitle'] = elementData.job_title;
window.sessionStorage['Date'] = elementData.entered_date;
**newOne.html**
function LoadCandidateDetail()
{
document.getElementById('Name').innerHTML = window.sessionStorage['Name'];
document.getElementById('JobTitle').innerHTML = window.sessionStorage["JobTitle"];
document.getElementById('Date').innerHTML = window.sessionStorage["Date"];
}

Path of Cookie not working

I want to create a cookie to store a class so I can use this through the whole site. I checked the cookies, and the cookie is created. The problem is when the cookie is set on another page the path is /url-page en not /
jQuery(document).ready(function($) {
values = $.map($('select option'), function(e) { return e.value; });
$('#color-select').on('change', function() {
$("body").removeClass(values.join(" ")).addClass( ("" + $('#color-select').val()) );
var foobar =("" + $('#color-select').val());
document.cookie = "gsscookie=" + foobar, "expires=;domain=;path=/";
})
});
I'm working on a local dev environment with the url plastic.dev.local. What am I doing wrong?
The value that gets assigned to document.cookie should be a single string, with a semicolon after the "key=value" assignment, and then semicolons between each property that you're setting on the cookie. The way you're doing it, with the comma after the first string, makes it so the second string is basically ignored. If you change it to document.cookie = "gsscookie=" + foobar + ";expires=;domain=;path=/";, it should work.

How to modify environment variables using WMI and JavaScript?

I need to access an environment variable and modify its value. I can access the variable using WQL ==>
wmi.ExecQuery("Select * from Win32_Environment Where name='Path' And UserName='<System>'");
However, I am not sure how to modify and save the value. I am using:
var reg = GetObject("winmgmts:/root/cimv2");
var paths = wmi.ExecQuery("Select * from Win32_Environment Where name='AA' And UserName='<System>'");
var items = new Enumerator(paths);
var path = items.item();
path.VariableValue = path.VariableValue + ";" + "random";
path.Put_(); //(as per first answer received)
But, I get this error:
Access denied
Code 80041003
Source SWbemObjectEx
I have UAC disabled, not sure what to do here.
Any help will be appreciated.
Thanks.
After you change the VariableValue, call Put_ to apply the changes:
path.VariableValue = path.VariableValue + ";" + "random";
path.Put_();

Categories