Javascript Help Passing Data via URL - javascript

Hi I wonder if someone could help me with this small issue I have the following code which I need to modify.
<script type="text/javascript">
function code(id) {
$('#myStyle').load('myphp.php?id=' + id);
}
</script>
I need to pass another variable into this code and add it to the GET part of the URL for example above it will include the URL myphp.php?id=124545
I want to add a second variable called num to the URL part but am confused what the code will need to become to make the correct post via GET
<script type="text/javascript">
function code(id,num) {
$('#myStyle').load('myphp.php?id=' + id); // how do I add the &num=124 for example
}
</script>
Thanks in advance

Simple. Use:
$('#myStyle').load('myphp.php?id=' + id + '&num=' + num);
Reference: http://www.quirksmode.org/js/strings.html#conc
Hope it helps!

Concatenate "&num="+num onto the string you already have:
<script type="text/javascript">
function code(id,num) {
$('#myStyle').load('myphp.php?id=' + id + "&num=" + num); // how do I add the &num=124 for example
}
</script>

Related

How do I reference a Razor variable later in my page in JavaScript or HTML

I have the following Razor code:
#{
if (IsPost)
{
string pn=Request.Form["pn"];
if (!string.IsNullOrEmpty(pn)){
/* my code to call the stored procedure with pn as a parameter*/
}
}
}
And in the page, using JavaScript / HTML I need to tell the user about pn. Be it that it's empty, or report back that the value was inserted in the database.
How do I do that?
You need to insert the result into the rendering. There are a couple of ways of doing that. You could store it in a variable:
<script type="text/javascript">
var pn = '#(pn)';
alert('pn is ' + pn);
</script>
Or you could call a function, sort of like JSONP:
<script type="text/javascript">
parsePn('#(pn)');
<!-- ... extra logic ... -->
function parsePn(pn) {
alert('pn is ' + pn);
}
</script>

Find the DOM location of a function call

I know that in Jquery you can get the event.id if I had triggered an event (click, etc) but how would I get the DOM location of a simple function call
Ex :
<pre id="container2">...
<script>
FunctionCall();
</script>
</pre>
I would like to get the "container2" value from inside my FunctionCall
In general, the script does not know where it was launched from so there is no generic way to do what you were asking. You will have to either find a container div that you know in advance or insert your own known object that you can then find.
In your specific example, you could do this:
<pre id="container2">...
<script>
var fCntr = fCntr || 1;
document.write('<div id="FunctionCallLocation' + fCntr + '"></div>');
FunctionCall(fCntr++);
</script>
</pre>
Then, from within the script, you can find the DOM element with the id that was passed to it.
Or, you could put the document.write() into the function itself so it marks its own location:
var fCntr = 1;
function FunctionCall() {
var myLoc = "FunctionCallLocation" + fCntr++;
document.write('<div id="' + myLoc + '"></div>');
var myLoc = document.getElementById(myLoc);
}
This exact code would only work if FunctionCall was only called at page load time so the document.write() would work as desired.
Maybe you could try add an id or class to the script element. So something like this:
<pre id='container2'>
<script id='location'>
(function FunctionCall() {
var container2 = document.getElementById('location').parentNode;
container2.appendChild(document.createElement('p'));
}())
</script>
</pre>

XML parse to HTML with jQuery

I have some little problem with my scripts. I want to parse xml file with some data elements to my html file. So I try to use jQuery but it doesn't work. Can you help me?
This is my xlm file:
<?xml version="2.0"?>
<choices xml:lang="PL">
<complete>Wskazówka</complete>
<temperature>300 stopni celcjusza</temperature>
</choices>
This is my jQuery code:
$(document).ready(function () {
$.get('../data/content_data.xml', function(data) {
var complete = $(data).find('complete').text();
var temperature = $(data).find('temperature').text();
$('div.menu_circle').text(complete + "|" + temperature);
});
});
I haven't tried this myself, but maybe it helps:
http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery
The issue is how try to output your variables. Try to append your variables to your element.
$(complete + "|" + temperature).appendTo('div.menu_circle');

populating div from external file div with javascript and jquery

Very frustrated here. Can't get this to work and don't know why.
I am trying to populate my div with a div from another html file. Here is my code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<div id="text">
content goes here
</div>
<script type="text/javascript">
function whenButtonClicked() {
var book = document.getElementById("book").value; //this is working
var chapter = document.getElementById("chapter").value; //this is working
var filename = 'files/' + book + chapter + '.html'; //this is working
$('#text').load(filename #source); //this is NOT
}
</script>
Thanks for any help.
Adam
Edit: Here is a link to the actual file I am working on. There may be something wrong in it as well.
Use this code: $('#text').load(filename + " #source");.

How to reload JSON with AJAX every 10 Seconds

I'm trying to reload a JSON file every 10 seconds with JQUERY.
The page is here: http://moemonty.com/chirp/chirp.html
The Code is here:
<html>
<head>
<title>the title</title>
<!-- included Jquery Library -->
<script type="text/javascript" src="./js/jquery-1.4.2.js"></script>
<!-- jquery library -->
</head>
<body>
<script>
$.ajaxSetup({ cache: false }); //disallows cachinge, so information should be new
function loadChirp(){ //start function
var url = "http://www.chirpradio.org/json";
$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D%22"+url+"%22&format=json&callback=?",
function(data){
console.log(data.query.results.json);
document.write('The artist is: ' + data.query.results.json.artist + '<br/><br/>');
document.write('The artist is: ' + data.query.results.json["record-label"] + '<br/><br/>' );
document.write('The album is: ' + data.query.results.json.album + '<br/><br/>');
document.write('The record label is: ' + data.query.results.json["record-label"] + '<br/><br/>');
document.write('The feedback link is: ' + data.query.results.json["feedback-link"] + '<br/><br/>');
document.write('The database id is: ' + data.query.results.json["database-id"] + '<br/><br/>');
document.write('The time is: ' + data.query.results.json.timestamp.time + ' ');
document.write(data.query.results.json.timestamp["am-pm"] + '<br/><br/>');
document.write('The current dj is: ' + data.query.results.json["current-dj"] + '<br/><br/>');
setTimeout("loadChirp()",5000);
alert('The timeout was triggered.');
});
} //end function
$(document).ready(function(){
//DOCUMENT READY FUNCTION
loadChirp();
});
//DOCUMENT READY FUNCTION
</script>
</body>
</html>
It doesn't seem to be working.
You probably want the previous set of returned data replaced by the new set, instead of appending it. In that case, using jQuery you can do:
<div id='content'></div>
<script>
function loadChirp(){
$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D%22"+url+"%22&format=json&callback=?",
function(data) {
$('#content').html('The artist is: ' + data.query.results.json.artist + '<br/><br/>');
});
setTimeout("loadChirp()",5000);
}
</script>
etc...
I would expect the loop to work as quoted, but there could be a subtlety around the fact you're using JSONP. I would change the setTimeout call to:
setTimeout(loadChirp, 5000);
...for a couple of reasons. First off, using the function reference rather than a code string is a better idea generally, and second off, you're quite certain that you're getting the right function reference (whereas with the string, what reference you get depends on the context in which the code is executed).
But as Pointy pointed out in a comment, there's a separate issue: document.write will not do what you probably want it to do there. You can only use document.write to write to the HTML stream that's being parsed as part of the original page load. After the page load, you can't use it anymore. Consider using jQuery's append or appendTo and similar functions to add to the DOM after page load.
You have an error in console.log(data.query.results.json); - console is not defined.
Also, you can use setInterval( "function()", 5000 );.
You should definitely use:
setInterval("loadChirp", 10000):
Don't write loadCrirp() inside setInterval as we're only passing a refrence

Categories

Resources