November 23, 2012

execute string as javascript function

Question by chugh97

My object has a call back:

var MyObject = {
CallBack: "function (whichSubMenuIsClicked, subMenuObjectTag) { self.doStuff(whichSubMenuIsClicked.SubMenuItem, whichSubMenuIsClicked.HeaderColumnName, whichSubMenuIsClicked.DivIdentifier);}",
}

The callback is a string. Now I need to execute it using MyObject.CallBack(param1, param2)
How can this be done using jquery or javascript. The self in this case is the original widget calling another widget. Thus the call back is on the original widget.

Answer by Starx

Very bad approach but You can do it by eval()

var MyObject = { CallBack: "AName = function(param1, param2) { alert(param1 + param2); }", }

eval(MyObject.CallBack+"(1,2)"); 

Demo

November 16, 2012

Block access to files and sub directory with .htaccess?

Question by jaya malladi

I have nearly 30 php files, 4 sub directories in my directory. I want to block some php files and sub directories from user in direct viewing like http://bhavani.com/hai.php

My currect htaccess file

## Enable Mod Rewrite, this is only required once in each .htaccess file
RewriteEngine On 
RewriteBase / 
## Test for access to includes directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /includes/ .*$ [NC] 
## Test that file requested has php extension 
RewriteCond %{REQUEST_FILENAME} ^.+.php$ 
## Forbid Access 
RewriteRule .* - [F,NS,L]

How to do it?

Answer by Starx

You can filter the files like this

<Files ~ ".php$">
  Deny from all
</Files>

And directory like this

<Directory "/subdirectory">
  Deny from all
</Directory>
November 14, 2012

Show delete message URL based on username

Question by user1793122

I have an PHP + Jquery wall message that show all message from anybody. Now I want to set delete URL in every messages. But the rule, user can’t see delete url if the messages is not his own.

.childs {
opacity: 0;
color: #000;
margin-left: 10px;
float: right;
}

.childs:hover {
opacity: 1.0;
text-decoration: underline;
cursor: pointer;
}

.parents:hover > .childs {
opacity: 1.0;
}


This logged in as Greg

<section class="parents">
Greg : Today is holiday
<div class="childs"><a href="delete.php">Delete</a></div>

This logged in as Jeremy

<section class="parents">
Jeremy : Beautiful day
<div class="childs"><a href="delete.php">Delete</a></div>

I want the Delete link show based on username logged in. So if Jeremy mouse over Greg message, delete link will not show because it’s not his message and if Greg mouse over his own message, he can see the delete url link and can delete it.

So how can I set that ?
Thanks for helps.

Answer by Starx

This is a simple approach. Store the logged in information of session using $_SESSIONthen compare this on your view class/code.

Like

<div class="childs">
<?php
if($_SESSION['logged_in_user'] == true) { 
   echo "<a href='delete.php'>Delete</a></div>";
}?></div>
November 11, 2012

Adding easing to the following jQuery animation?

Question by alexchenco

I’m animating the height of element:

  // Animate height of items
  $j(".item .row").toggle(function(){
    $j(this).animate({height:500},200);
  },function(){
    $j(this).animate({height:300},200);
  });

I was wondering how to add easing to it? (e.g. the animation slowing down towards the end?)

Answer by Starx

Define easing, the following way

$('selector').animate({
    prop:1
},{
    easing: easing, //Something like 'linear'
    duration: duration,
    complete: callback
});

You can add other easing effects also, by including the Easing Plugin.

In your case, it would be something like

$j(".item .row").toggle(function(){
    $j(this).animate({height:500}, {
        easing: 'linear',
        duration: '200'
    });
  },function(){
    $j(this).animate({height:300},{
        easing: 'linear',
        duration: '200'
    });
});
November 9, 2012

Remove scrollbar but not scrolling functionality

Question by Max

I know you can define overflow:hidden; on the body of the HTML to remove the scrollbar, but I would like to still be able to scroll with the arrows or scroll wheel on a mouse.

Thanks in advance for any help.

Edit:
Thanks for all the advice about on hover scrollbars and custom bars. Also thank you for all concerns about impacting users experience by removing the scrollbars. I shall elaborate a little more so you explain where I am coming from.

I have a circular page (if you scroll with a scroll wheel or arrow button, when it reaches the bottom it resets to the top of the page and starts again). A never ending loop. A scrollbar impacts on this as a bar is limited and when it reaches the bottom and resets to the top the users mouse is still at the bottom of the page meaning when they move it there is some flickering between the top and bottom of the page.

I plan to remove the scroll bar and replace it with arrow buttons at the top and the bottom of the window. This is why I would like to remove the scrollbar completely but leave the scrolling functionality.

Answer by Starx

Ok, new answer. I just developed a little trick to do so, mixed with jQuery.

Create a wrapper div inside the body, with the following css.

body { overflow: hidden; }

#wrapper { overflow: auto; }

Then, simply set their respective heights:

$("body").height($(window).height());
$("#wrapper").height($("#text").height());

Demo


To support for resizes

$(window).trigger('scroll');

$(window).scroll(function() {
    $("body").height($(window).height());
    $("#wrapper").height($("#text").height());    
});

Demo

a href link with basic authentication

Question by Anna Smother

I have a website were I login via basic authentication, that works fine. But when I want to download a .pdf file (that is generated by the server) I get a popup with the information that I need to login…again :/.

How can I prevent this? Or how can I send the basic authentication information with the link? I saw some examples, like: http://username:password@yourserver.com/filename.pdf . But that don’t work in IE.

How can I solve this with only html and or jQuery?

edit:
I login via a form in the site, that works. But when I want to download an .pdf file while i’m logged in, I get the pop-up.

<a href="http://yourserver.com/yourpdffile.pdf">Report1.pdf</a>

Answer by Starx

Basic authentication is Server based, HTML and jQuery are helpless here.

However, before sending AJAX request to such page, you can provide these details. Here is an example.

$.ajax({
    'url': 'http://yourdomain.com/action/',
    'beforeSend': function(xhr) {
        xhr.setRequestHeader("Authorization", "Basic " + encodeBase64(username + ":" + password) 
    },
    //.....
});

List connected files as array when we call a file with php?

Question by AnzaVR

What I am trying to achieve, when I open a website, it starts a network activity by loading images, swf files, css file etc. Is there any way in php to get a list of those resources as array?

Answer by Starx

You can use regular expression to parse the links once you have all HTML markups.

Here is an example to get the CSS request:

$regexp = "<links[^>]*href=("??)([^" >]*?)\1[^>]*/>"; 
preg_match_all("/$regexp/siU", $input, $matches);
var_dump($matches);

In the same way get scripts resources, image request, and iframe request.

Extending the Session Library in Codeigniter 3

Question by luv2Code

I’ve been using CI for a while now and recently upgraded to CI 3. I noticed the Session library has now been moved to a folder.
I used to have my own MY_Session.php file in the application/libraries folder that extended the default CI library.

I also use the autoload.php file to autoload my session library.
This no longer works, as I get Unable to load the requested class: Session.

If I remove MY_Session.php file, then the pages load, but then I’ll be missing my extended functionality.

Does anyone know how exactly to extend the session library in CI 3?

Answer by Starx

You can do this similar to extending other core components.

class MY_Session extends CI_Session {

    function __construct() 
    {
        parent::__construct();
        echo "In extended the session";
    }
}  

Make sure you load the session library as well. Like

$autoload['libraries'] = array('database','session');

Also, Unable to load the requested class: Session are generally triggered for the two reasons.

  • CI can’t fine the session
  • You haven’t autoloaded the library

Also, make sure you have a encryption key on your config.php

$config['encryption_key'] = 'xxxxxxxxxxxxxxxxxxxxxxx';
November 8, 2012

Using onChange on Select Option

Question by deetu

I have a select option box that is dynamically populated, so if i change the select some other boxes are filled, and i can delete them too.
Which also deletes the option from the select box, this works fine, but when i delete the options and i have just one left in the box, there seem to be no way to fire the option.

$("#email_name").on("change", function(){
    var componentkeys = myDailyemail.getKeys($("#Component").val());
    $.each(componentkeys, function(kIndex, kItem) {
        $("#key").append($("<option/>").val(kItem).text(kItem.toUpperCase()));
    });
});

Answer by iMoses

You can automatically trigger a change event after you populated the select element, making it as if the user already selected the first option.

$("#email_name").on("change", function(){
    var componentkeys = myDailyemail.getKeys($("#Component").val());
    $.each(componentkeys, function(kIndex, kItem) {
        $("#key").append($("<option/>").val(kItem).text(kItem.toUpperCase()));
    });
    $("#key").trigger("change"); // trigger key
}).trigger("change"); // trigger email_name

I wasn’t sure what you wanted to trigger so I entered both possibilities.

Answer by Starx

You can use .trigger() to fire events manually.

Insert following, where needed.

$("#email_name").trigger('change');
November 7, 2012

How send with post method in code behind?

Question by mum

I have a param A and its 25000 characters long.
How to send param A with GET method in php?

 $kq=0;
 $msg1= strtr(base64_encode($msg), '+/=', '-_,');              
 $Title1=strtr(base64_encode($Title), '+/=', '-_,'); 
header("Location:".JRoute::_('./index.php/i0702?'.'kq='.$kq."&Title=".$Title1."&Msg=".$msg1));

Note: strlen($msg1)> 25000 characters. Now, How send with post method?

Answer by Starx

The maximum characters you can send from GET method is 3000. Use POST method.

<form method="POST" action="your/url/to/open.php">

    <input type="text" value="<?php echo $a; ?>" />
    <input type="submit" />

</form>
...

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