set javascript intial variable to data scraped from website - javascript

I am trying to set a variable in javascript to a value retrieved from another source. For instance, in this case, I have a calculator that is calculating estimated payments. Currently initial variables are set as follows....
// Initial Values<br>
var data; // To be used by chart<br>
var homePrice = 250000;<br>
var downPaymentPercent = 3.5;<br>
var interestRate = 5.25;<br>
var user_state = "FL";<br>
Two questions I have are...
1.) What would be the easiest way to set the interestRate variable equal to todays 'current rates' as shown by an external website such as...
www.wellsfargo.com/mortgage/rates/purchase-assumptions?prod=1
Also open to learning of a better way if there is a more straight forward method than this
2.) user_state is currently set through this initialisation list, what method would be used to set this state based on a users IP address?
My main interest is in solving question 1 right now, I am curious to question 2, but the first question is more important at the moment.
Thanks!

Related

Passing multiple dynamic values from a web form to SSRS

I have inherited code that was written by someone else. The code takes a "number" and passes it to an SSRS report. I have modified the report to include 5 more alpha numeric strings that I need passed to the report. Here is the initial code:
var h = "SSRSRepBrowserParameters.aspx?rs:ID=b2d543c2-1cce-487b-a3c0-743baa5933e1&QuoteNumberParameter=";
var q = $('.HdrTitle').eq(1).html();
var x;
x = q.split(" - ")\[1\];
x = x.replace('.','');
x = x.replace(/^0+/, '');
x = x.replace('.','');
$("#btnPrintVendReq").ready(function(){ $("#btnPrintVendReq").click(function(){
window.open(h+x '\_self');});});
I surprisingly understand what the guy did using variables to make this:
\*\*class="HdrTitle"\> Quote Form - 00.019.000 \*\*
look like this:
19000
What I need to do is add 5 more dynamic values that are in the web form and append them to the URL passed to SSRS.
In the web form the values are FreeTextField_10...11...12...13...14 and will be passed into the URL as ItemNo1...ItemNo2...ItemNo3...ItemNo4...ItemNo5:
\*\*class="saveHistory Text" name="FreeTextField_10" id="FreeTextField_10" value="2764-06" \*\*
So if all that mess makes sense the end result I need the window.open to pass a URL that shows:
SSRSRepBrowserParameters.aspx?rs:ID=b2d543c2-1cce-487b-a3c0-743baa5933e1&QuoteNumberParameter=19000&ItemNo1=2764-06&ItemNo2=2764-06&ItemNo3=2764-06&ItemNo4=2764-06&ItemNo5=2764-06
I was actually impressed that I was able to figure out what I need and hard coding it works...but unfortunately the vales come from a web form and will change so it's the pesky variables that throw me off here.
Unfortunately I have not been able to come up with the correct syntax to successfully pass the variables to the report.

How can I add permanent data into an variable

I'm working on a project that plays a game of rock paper scissors
the game works fine but i want to add something into it. a pointing system where it will add 1 each time a player win but i can't seem to make this work i've been trying to find workarounds for almost 2 day now but i can't seem to find any solution. The thing is the code works but after i log the function that adds 1 to a variable but after that it returns to it's original value which is 0.
const one = 1
function addone(test){
test += 1
}
addone(one)
console.log(one)
This print the original value like the function did not do anything to it. im so confused.
I see two problems in your code:
one is a const variable. That means that you can't modify its initial value.
test is a local variable inside your function, that means function won't modify the values of the variable that you pass as parameter (one).
There is an operator(++) that add a unit, I highly recommend you to use it instead of make a function:
var one = 1
one++;
console.log(one)
If you want to use a function you have to do like this:
var one = 1
function addone(test){
return test+1;
}
one = addone(one);
console.log(one)
In terms to store permanent data into a javascript variable, that's not possible. Variable are stored in RAM, that means that when you stop the program, al data of your program are wiped. Permanent store are made in database and other complex data structures.
If you want to play multiple games, you should consider to allow the players to play more games before the program finish.
thank you very much i never think that i could get more dumb so i cant manipulate it because it's just a copy of that variable that im inputing as parameter? so it means that the function did not touch the variable at all. I wish i could go back to my mother's womb.
and the const it's just a typo very sorry for this.
i did not know this website is very active i thought i would wait days just to get a response thank you very much and have a nice day

How to use javascript split in Qualtrics

Very new to Javascript and have been searching the webs for assistance, but haven't quite found a solution.
I am attempting to use javascript to split/remove the output of a particular field. The data in the survey is being pulled from our school's database after a user logs in to the survey via shibboleth. All the information is being displayed, so that part works, but one particular field is appending an email address (#email.com) to a field.
I want to omit this part from being displayed. Either my javascript is incorrect or the javascript is not being loaded/read. The javascript code was borrowed from a colleague and it works on his surveys, but he has a lot of other things going on in his survey and this works for him.
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var iid = "${e://Field/theUTIID}";
var split_array = iid.split("#",1);
var eid = split_array[0];
Qualtrics.SurveyEngine.setEmbeddedData('theUTIID', eid);
});
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var iid = "${e://Field/theUTIID}";
var split_array = iid.split("#",1);
var eid = split_array[0];
Qualtrics.SurveyEngine.setEmbeddedData('theUTIID', eid);
});
I have this in both the Onload and OnReady for testing. Doesn't matter if I have this is one location or the other, I am not getting the desired results.
I only have one question on the survey (it's just a test survey) and so the javascript code is with the first and only question.
Survey Question has the following in a text entry. Again, output is displayed, but need the #email.com to removed from the EID field.
The code looks correct (other than it only needs to be in one or the other function). I'm guessing it isn't a problem with the code, but where you are trying to pipe the embedded variable. The JavaScript above has to be attached to a question on a separate page before the place where you want to pipe it.
Add a page break, then pipe theUTIID into a question on the next page.

updating global variables javascript/html [duplicate]

This question already has answers here:
Persist variables between page loads
(4 answers)
Closed 7 years ago.
so currently I am building a website where I get data from google spreadsheets and then graph it on my website...
However, because this data is constantly streamed from somewhere else, I need to be able to show 10 data points on the graph every time. (So shows 10 data points then the next 10 and then the next 10 and so on). So I personally need 2 global variables to keep track of where in the data I am thus be able to present the user with the correct graph.
I have tried this...
<script>
var i = 0;
var lengtharray = 0; //This variable is the second global variable that I need and is updated within the same function as i.
function doSomething(){
anotherFunction();
i = i + 10;
lengtharray++;
}
</script>
so the next time the script is run to show the next data point on the graph, the i value that I expect is the previous i value + 10 and the lengtharray value to be incremented...
instead, what I get is i = 0 again and lengtharray = 0 again..
I need these values to be "updated" and only initialised/reset when I want it to be/the browser closed and reopened on the webpage...
I hope this question all makes sense!
Thanks in advance.
If the problem is due to those globals and the function being re-instantiated every time then you might just be able to use a singleton pattern to prevent this. If you are unfamiliar, it is a design pattern that always returns the previous instance of your class, variable, whatever. That way there is always only one instance of your object, class, variables etc. I have linked the implementation in javascript here

Passing a javascript variable on to the next page WITHOUT server-side interference?

I'm making a web-app with PhoneGap. Here's my current hurdle:
On page one, the user chooses a latitude and longitude, then hits continue. With some nice GET action on the form and a javascript URL parser on the next page, the next page thus is passed the lat and lng coordinates. On this new page, the user is asked to input a radius. Via the same method, the radius is passed on to the final page, which displays a google map with a circle on it made from the coordinates and the radius.
But the problem is that when I pass the radius on to the final page, it obviously only passes the radius. How can I pass the lat and lng onwards from the second page, which aren't part of form information? They're already established. I know the obvious solution is to put all three variables on one page, but for foolish reasons beyond my control it needs to be this way.
So the question stands: how do you pass information that exists as a variable in javascript on to the next page? It's not part of a form. Just a variable.
All you need to do is add a couple hidden input elements to the form (<input type="hidden" />).
This can be done many ways. The shortest is to use a library (example code happens to be jQuery):
$('<input type="hidden" name="lat" />').val(latitudeValue).appendTo(formSelector);
$('<input type="hidden" name="lng" />').val(longitudeValue).appendTo(formSelector);
Raw JS is a bit longer, but not particularly complicated:
//I haven't checked this, so some cross-browser tweaking might be necessary
var lat,
lng,
form;
lat = document.createElement('input');
lat.type = 'hidden';
lat.value = latitudeValue;
lng = document.createElement('input');
lng.type = 'hidden';
lng.value = longitudeValue;
form = document.querySelector(formSelector);
form.appendChild(lat);
form.appendChild(lng);
latitudeValue, longitudeValue, and formSelector are variables that you'd have to define in the context of your page. You might also use a different parameter name, such as name="longitude".
It sounds like you really should be using AJAX to change the content of the page, so that you're not making consecutive GET requests. Instead, you could simply be swapping out the main content of the page depending on which step in the process the user is on. It would remove the necessity to do any URL parsing (which is easy to do incorrectly).
You can access get data in Javascript. Hence, just pass your data into the next page as get data in the URL and parse it as follows with Javascript.
Where your URL contains ?foo=1&bar=2 at the end, you can parse foo and bar with
var parts = window.location.search.substr(1).split("&");
var $_GET = {};
for (var i = 0; i < parts.length; i++) {
var temp = parts[i].split("=");
$_GET[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]);
}
alert($_GET['foo']); // 1
alert($_GET.bar); // 2
Easier option would be to create a cookie using Javascript/jQuery and add the values to it. So you can add all the values on every page and then finally retrieve the data you want, just from the cookie.
You use no server side scripting yet you get all the values you need.

Categories

Resources