October 31, 2010

jQuery vs. javascript?

Question by Adam Kiss

I recently stumbled upon some javascript forums (sadly, link is lost somewhere in the universe), where you could feel real hate against jQuery for not being… any good?

Most arguments seem to make sense actually.

Now, I really like jQuery, mostly for letting me concentrate on things I want to do rather on browser inconsistencies and it actually makes AJAXing with cool (or overused?) effects fun.

But if really is something rotten in the core of jQuery, I don’t want to rely on it the way I actually… rely on it.

I don’t want to start yet another argument over which framework is the best… but… Which framework is the best (joke)? As a case usage, think about small to medium web and it’s administration.

I’m just trying to figure out, if stuff in some framework or pure javascript with few mine functions really makes difference.

Edit:

I actually tried to had a normal objective discusssion over pros and cons of 1., using framework over pure javascript and 2., jquery vs. others, since jQuery seems to be easiest to work with with quickest learning curve. However, some people just don’t understand it and think that I’m starting yet another flame (what I am not). I am actually voting to reopen this question.

Also I’m really interested in:

  • Does jQuery heavily rely on browser sniffing? Could be that potential problem in future? Why?
  • I found plenty JS-selector engines, are there any AJAX and FX libraries?
  • Is there any reason (besides browser sniffing and personal “hate” against John Resig) why jQuery is wrong?

jQuery actually, as most used, stands for other frameworks also.

Answer by Xr.

It’s all about performance and development speed. Of course, if you are a good programmer and design something that is really tailored to your needs, you might achieve better performance that if you had used a Javascript framework. But do you have the time to do it all by yourself?

My personal opinion is that Javascript is incredibly useful and overused, but that if you really need it, a framework is the way to go.

Now comes the choice of the framework. For what benchmarks are worth, you can find one at http://ejohn.org/files/142/ . It also depends on which plugins are available and what you intend to do with them. I started using jQuery because it seemed to be maintained and well featured, even though it wasn’t the fastest at that moment. I do not regret it but I didn’t test anything else since then.

Answer by Starx

Jquery VS javascript, I am completely against the OP in this question. Comparison happens with two similar things, not in such case.

Jquery is Javascript. A javascript library to reduce vague coding, collection commonly used javascript functions which has proven to help in efficient and fast coding.

Javascript is the source, the actual scripts that browser responds to.

October 29, 2010

div in front of another resets jQuery hover animation. how to prevent this?

Question by MauF

i have an animate effect applied to 4 divs in order to create a blinds effect: when you hover over the first box it grows , the other 3 shrink and when you “mouseout” all 4 return to 25% of the total height.

i want to insert a nested div inside each box (in front, actually with absolute positioning) in order to present some text and links, but every time the mouse hovers over this new div, the animation resets (the mouseout event runs).

how can i make the nested div invisible to the animation? or include it such way that it is invisible to the animation code but the urls work properly?

the example is here:

jquery problem

all code is inline for this demo.

thanx in advance.

Answer by Starx

Solved, check here

The trick to add the selector $("#one > *")

October 28, 2010

Find who calls a method in PHP's class

Question by balkon_smoke

Possible Duplicate:
Find out which class called a method in another class.

Hello everyone,

I have a class and I can’t find where his object creates.
I mean, can I somehow find out who calls his constructor?

Thanks a lot,
Andrey.

Answer by Starx

use

$trace = debug_backtrace();
echo "<pre>".print_r($trace[1])."</pre>"; 
//see all the displays '1' is the referrer '0' is self
echo $callingfunction = $trace[1]['function'];
echo $callingclass = $trace[1]['class'];

jQuery remove elements by condition

Question by plutov

I have some tr elements in table:

<table>
<tr id="tr_level_1">...</tr>
<tr id="tr_level_2">...</tr>
<tr id="tr_level_3">...</tr>
<tr id="tr_level_4">...</tr>
<tr id="tr_level_5">...</tr>
</table>

In Javascript I have the next variable:

var levels = 3;

I want to delete all tr’s where number in id is more than levels. And if levels is more than number of tr’s – adding tr’s after last.

Thanls a lot.

Answer by netadictos

Working demo

Try this:

var levels = 3;
$("table tr:gt("+(levels-1)+")").remove();

I substract one because this expression (“gt”: greater than) is 0-based index.

For the second part of your question try this:

http://www.jsfiddle.net/dactivo/fADHL/

if($("table tr").length<levels){
 //the code here for less than levels
}
else
{
 $("table tr:gt("+(levels-1)+")").remove();    
}

Answer by Starx

try this

var total = $("#table").find('tr').length;
var levels = 3;
if(levels<=total) {
   for(levels=levels;levels<=total;levels++) {
       $("#tr_level_"+levels).remove();
   }
}
else {
   $("#table").append("<tr id="tr_level_"+total+1+"">..</tr>");
   // this will add the element with tr_level_6 at the end 

}
October 27, 2010

passing variables from php file to anther

Question by OdO

How to pass variables from a php file to another while it is not html inputs ,just i have a link refer to the other file and i want to pass variables or values to it

Example:

File1.php

<?php

$name='OdO';
echo "<a href='File2.php'>Go To File2</a>";

?>

File2.php

<?php

echo $name;

?>

Answer by kijin

Use sessions to store any small value that needs to persist over several requests.

File1.php:

session_start();
$_SESSION['var'] = 'foo';

File2.php:

session_start();
$var = $_SESSION['var'];  // $var becomes 'foo'

Answer by Starx

You can use URLs to pass the value too.

like

index.php?id=1&value=certain

and access it later like

$id = $_GET['id'];
$value = $_GET['value'];

However, POST might be much reliable. Sessions/Cookies and database might be used to make the values globally available.

October 26, 2010

Javascript resize text as an image

Question by Andrew Jones

I was wondering how to resize a div containing some text by setting a width and height so it would stretch to fill that area like an tag? E.g. when you set the width and height attribute of an image, it resizes the image to fill that area. I need to take an arbitrary width and height value and stretch a div containing text to those dimensions. I do not wish to send a request to a php script to build an image containing the text since it will be slow and affect the scalability of the web app. Oh, and I would preferably not like to use html5 because it is a requisite that I need compatibility with ie7+ (and firefox/chrome).

Thanks.

Answer by Starx

Ok, Here is how to do that.

http://www.jsfiddle.net/Zp8AH/

However, some factors like Font Family, Division width have to be fixed, in order for this code to work.

October 24, 2010

php session management

Question by AJCE

I am developing a system in php which has different types of users like admin, A, B, C.

I need to allow log in just one user from the type at a time. Means
1) if there is already an admin user is logged in, no other admin user can log in.
2) And the logged in credentials cannot be used on other pc at the same time (just like yahoo messenger login system).

I have tried to store session with login logout time in database table when someone login and logout.
But it creates problem when someone close the browser without logging out, the logout entry time misses.

Answer by Starx

1) if there is already an admin user is logged in, no other admin user can log in.

If you want to stop multiple logins from same system then set a session flag like $_SESSION[‘loggedin’]=true; while a successful log in occurs, then before each login attempt, check this flag and only when the flag is falseproceed with the login process.

If you want to stop multiple logins from multiple system then create a temporary table in your database to hold the status of your logged-in users and before any login attempts occur check this table to find any logged in user, if you dont find any user then only proceed, or else prompt the user

2) And the logged in credentials cannot be used on other pc at the same time (just like yahoo messenger login system).

create a temporary table in your database, which will hold the status of your all logged in users, then when a user who is already logged-in, attempts to log in from another place, check the status of that userid in your temporary table, if he is already found logged-in then either prompt him or deny him or log him off from other computer before logging him in again.

October 11, 2010

want to change color at multiple places in one go !

Question by Rahul Utb

My problem is I have couple of divs in my page. All have header of similar color. Now if I have to change the color(for example background color) of all divs, I have to make changes as many divs I have. Is it not possible to just change or say write the color code at one place (like in a variable) and the then use that variable as color value in the embedded styles to all those divs. Something like javascript entities.

Answer by Yi Jiang

If you need variables in CSS, you might want to look at CSS pre-compilers (is this the correct term?), such as Sass, which does this Server-side and eases the pains for having many different color repeated across multiple rulesets.

Otherwise, when developing, try splitting your CSS files into individual components, such as typography.css, color.css etc. to help better organise them. You’ll still want to combine them after development is complete for better performance, but doing this does help keep things neater and tidier.

Lastly, you can always define large rules like this:

#header, #footer, #nav, #sidebar {
    color: orange; /* I like orange! */
}

Which would reduce redundancy somewhat. Using Javascript for styling and presentation should only be kept as a last resort; there are always tools available to keep your CSS tidy; you only need to use them.

Answer by Starx

Jquery solution

define all the divs with a specific class like

<div class="changeable"></div>

Then use jquery to change the background

$(".change").click(function() {
    $(".changeable").css("background","#000");
});
...

Please fill the form - I will response as fast as I can!