PDA

View Full Version : few js questions



rich1812
Feb 5th, 2017, 03:02 PM
Hello, I have a slider script. In the script it is like this


<script>
$(function(){
$("#slides").slidesjs({
start: 3,
width: 940,
height: 10
});
});
</script>

Once the html or php is loaded, the slider starts, all is well.

Question1: No where in the page explicitly tells this function to load, how does it get loaded? is the $ sign in front of $(function() some kind of shorthand from jQuery for document .ready or something like that?

Question2:
$("#slides").slidesjs() function, the variable start:3, tells the function to start on slide 3, now I would like to have a parameter like start: xyz, to pass in so that I can just pass any number, the script will start on that number. perhaps some code like this something like:

Question3: when I put an alert() right after $(function(){, the alert pops up with this message: function (a,b){return new m.fn.init(a,b)}
What does it mean?



<script>
var sf=6,
$(function(sf){
$("#slides").slidesjs({
start: sf,
width: 940,
height: 10
});
});
</script>

How to go about doing this?

sunfighter
Feb 5th, 2017, 06:20 PM
Question1
$(function(){... is shorthand for $( document ).ready(function() {... which runs the code inside of it as soon as the page loads.

Question2
Sounds counter productive. If your going to write code to pass the start pic. into the function why not just change the 3 to what you want.

Question3
Who knows, it doesn't happen to me, but I don't have slidesjs() install; which is were it's coming from. Looks like your not passing into the $("#slides").slidesjs({...}. which your not.
-------------------------------------

Extra
the $ means jquery. That's the first thing you learn if you study jquery.

This line in your second code section is wrong:


var sf=6,


If you had learned javascript you would know that a sstatement ends with a semicolon and the line should be:


var sf = 6;


Next you don;t pass variables into the $(function(sf), so leave the line as $(function()

If you do those two things the code might work. Try it.

rich1812
Feb 5th, 2017, 08:46 PM
Haha, I kinda figure that out seems JQ always use the $ for short. They must really like money. :}
As for the second one, "why not just change the 3 to what you want." cuz the slide number is stored in database, too that went the user comes back, they will have where they left off from last visit. Soon after I posted, I kinda figured out. all I needed to do is



<script>
var sf=<?php echo $slideFrame?>
$(function(){
$("#slides").slidesjs({
start: sf,
width: 940,
height: 10
});
});
</script>
No need to passed in a parameter. Well, that was easy.;)

The third is weird tho, cuz it doesn't happen in Chrome or Firefox, perhaps it's browser thing, I use Safari by the way.

Thank you as always.

Dormilich
Feb 6th, 2017, 08:53 AM
Next you don;t pass variables into the $(function(sf), so leave the line as $(function()

Reason being that jquery always passes itself as parameter, that is, sf would be equivalent to $.

rich1812
Feb 6th, 2017, 03:14 PM
Thanks for the reply.
That's something confuses me the most. As a beginner learn ajax in plain old javascript, so many examples I read online always throw in some jquery stuff, Sometimes I got so lost between the two because the syntax look similar. I do however realised that any thing with $ is most likely jQuery.

While I am at it, here is a question. The ajax code below, I need to send the sf valuable to the same page-ajax_GET.php where ajax makes its call, this.responseText always returns the codes of the entire page. I trY to avoid so I make the element hidden, <span id="slide " style="visibility: hidden;">Val</span>. I really only need it to return the $_GET['sf'] key value pair. I read that In jQuery .ajax method, I can choose datType as plainObjest and I read that it only returns key-value pair, If that is the case, how can I set the dataType in javascript ajax without the document.getElementById("slide").innerHTML?



<script>
<?php echo $_GET{'sf']; ?>
<span id="slide " style="visibility: hidden;">Val</span>

function ajax_frame_count(str){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {

if (this.readyState == 4 && this.status == 200) {
document.getElementById("slide").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "ajax_GET.php?sf=" + str, true);
xmlhttp.send();

};
</script>


Thanks

Dormilich
Feb 6th, 2017, 03:24 PM
I need to send the sf valuable to the same page-ajax_GET.php where ajax makes its call, this.responseText always returns the codes of the entire page. [...] I really only need it to return the $_GET['sf'] key value pair.
you can't make the same script return two completely different responses.

rich1812
Feb 6th, 2017, 04:56 PM
you can't make the same script return two completely different responses.

Yes, I understand that, what I meant is that is there anyway to just get the $_GET['sf'] without getting the entire html code from this.responseText or by other means. Does it have anything to do with the dataType? I am still new to js, but little by little I start understand.

Dormilich
Feb 6th, 2017, 05:02 PM
what I meant is that is there anyway to just get the $_GET['sf'] without getting the entire html code from this.responseText

I don't follow you ... what does PHP input has to do with AJAX response?

rich1812
Feb 6th, 2017, 05:57 PM
in an url like <a href ="xyz.php?data=123"...> click here</a> for example. It loads the page xyz.php In the xyz.php page, I can get the variable data by puting a $x=$_GET['data'] in xyz.php and I get the number 123.
How can I do this in ajax?the same result in ajax. This line in ajax xmlhttp.open("GET", "xyz.php?d="+ '123', true); doesn't it put data in the $_GET['d']? How would page xyz.php get the data in ajax?

Dormilich
Feb 6th, 2017, 06:06 PM
How would page xyz.php get the data in ajax?
same way as with no AJAX. HTTP is HTTP, no matter if it comes from a page load, a form submit, or AJAX.

rich1812
Feb 6th, 2017, 07:18 PM
Ok, I got it now, it has not thig to do with this.responseText, I even comment out the document.getElementById("ajax").innerHTML = this.responseText; it works as I expected.:)
No clumsy id=xxx" ellements needed , sweet! :p