The query string seems to affect page loading speed - javascript

I'm working on this.
It's hosted on c9.io, a browser based collaborative programming IDE
I have an index.php with a small script:
<?php
$path = ltrim($_SERVER['REQUEST_URI'], '/');
$elements = explode('/', $path);
if(is_numeric($elements[0])){
require_once 'post.php';
}elseif($elements[0]==''){
require_once 'main.php';
}else{
require_once '404.php';
}
?>
When there is nothing in the url (only https://dickbutt-etaoin.c9.io ) it displays the page pretty much instantly but keeps loading for another five seconds during which none of the javascript on the page works
When I have a few numbers (for instance https://dickbutt-etaoin.c9.io/241 ) it's about as fast as it gets.
How can I make both the pages load at a normal speed?
I've tried reversing the order of if statements and it doesn't change a thing. Both the pages are relatively big, contain both php and javascript and a giant css file.

After some prodding in the network tab and googling, I found my answer.
I added this line of code to index.php
header('Connection: close');
Now it does what I wanted it to do. Thank you all for trying to help!

Related

Redirect Using JavaScript and PHP

I am using below code for redirect users.
<?php
$redirect_to = "https://google.com";
$redirect = "https://yahoo.com";
echo "<script>location.href = '".$redirect_to."';</script>";
header("Location: $redirect");
exit();
?>
I prefer to use javascript redirect, but in somecase when user have not javascript enabled in his browser, I am using PHP redirect as backup redirect but sometime I am getting header already sent error on php redirect code and sometime I am not getting that error.
if I use exit() after redirect by javascript, I will not able to
redirect user if he have javascript disabled in his browser.
my question is there any way to stop php code if javascript redirect was success? I am not getting idea what I have to do for handle my situation.
Let me know if any expert can help me to solve puzzle.
Thanks!
Well, you are a web developer using PHP and Javascript.
One thing to remember, PHP code always runs first on the server. It's a server-side language, so no matter how you arrange your lines of code, your php code will finish running. Then, the client source code (HTML, CSS, JavaScript) is sent to the browser and executed on it (Client-side).
And next, the job of redirecting is always on the client side, the server doesn't actually redirect people to another site. What actually happens is that your php source code generates a redirect command that sends the browser for a redirect.
Another thing, in php, any work related to the header() function needs to be executed before any content is generated. That is, it must run at the top, before any method echo, exit, print,... or content block.
When deploying a php application, if you already use header("Location: ....") then it makes no sense to use javascript directives, as the browser will prioritize handling the redirect in the header first! !
The source code should really be:
<?php
$redirect = "https://yahoo.com";
header("Location: $redirect");
?>
Another way if you still prefer using javascript redirects and are compatible with javascript disabled, that is to use the tag meta[http-equiv="refresh"]. Refer to the following example:
<?php
$redirect_to = "https://google.com";
?>
<meta http-equiv="refresh" content="5;URL='<?php echo $redirect_to; ?>'" />
<script>location.href = "<?php echo $redirect_to; ?>";</script>
The number 5 appears in the content of the meta specifying the countdown (seconds) the browser will redirect when the page has finished loading. When the page cannot execute javascript, after 5 seconds the page will redirect.

How do you add dynamic anchor text in wordpress links.

We would like to have links in a word press site that have the current meta description of the target site as the anchor text of the link.
I understand this requires either javascript or php and am not sure which is the appropriate approach and which is most easily supported within word press.
If you have Wordpress then you should have cURL installed and activated (or find the way). Also, there is a PHP function called get_meta_tags(). So, you could do something like this assuming you have an array of links with each URL called $links_array:
foreach($links_array as $link){
$tags = get_meta_tags($link);
$description = #$tags['description'];
//Printing each link
echo "<a href='$link'>$description</a>";
}
Interesting question and yes it is possible. You can't do it with javascript or AJAX because the browsers' cross-domain policy won't allow you to do this. I think it has to be a combination of both.
The first solution that i can think of is creating some kind of proxy with PHP, that returns the contents of the targeted URL (the one you link to):
<?php
$url=$_POST['url'];
if($url!="")
echo file_get_contents($url);
?>
Lets say we call this little script "getit.php". Now you can get a AJAX call going, that sends the target url to your .php file and the .php file returns the content of the targeted page. Then you are going to extract the description meta-tag from the returned data.
Of course you could get it in the PHP file and only return the meta description, because that would even be a better solution. You could try something like this in the PHP:
<?php
$url=$_POST['url'];
$tags = get_meta_tags($url);
return $tags['description'];
?>
PS. Apologies for my bad English, it's not my native language.

Magento javascript bundling - turn off for page

I have a Magento site setup and I did have Javascript bundling turned on.
This worked very well. Apart from one page - the PyaPal review page. A critical page. After a few days of slow sales, I saw a javascript error on this page. Turned off bundling and all works.
Is there any way I can turn off the bundling for this page only?
Thanks in advance!
It seems like the paypal page may included some js files also. So it may be jQuery conflict. Better you should fix this like adding "no conflict function"..And you dont want do that and as per question you can just simply prevent to load that js files that paypal page only. By simple condition you can achieve this. Check your url by if condition like this,
<?php
// for example
$file = '<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>';
$url = Mage::helper('core/url')->getCurrentUrl();
// OR
$url = $_SERVER['REQUEST_URI'];
if($url != "paypla_review.php") { //your paypal page
echo $file;
}
?>
If you try to include js lib files then Remember that you should include this code any header page. i.e. You should include js lib file first always.

jQuery + Ajax solution for "browsing" very large files

I've got some potentially very large (multiple GB) text files that I'd like to be able to view within a div.
In concept it's not hard... make an AJAX request for a more sensibly sized chunk of file, the AJAX script (PHP would be nice) seeks to an intelligent position within the file, and sends me what I need, which is then rendered vis JS in to an element.
This can't be a new problem, and I'm hoping someone else has already solved it.
Is there such a bit of Javascript (jQuery preferably) with related server scripts already out there somewhere?
I don't know of any library which does this. But it shouldn't be too hard doing yourself. The server side is almost trivial:
<?php
$chunkSize = $_GET['chunksize'];
$offset = $_GET['offset'];
$handle = fopen("text_file.txt", "r");
fseek($handle, $offset);
$chunk = fread($handle, $chunkSize);
fclose($handle);
header('Content-Type: text/plain');
echo $chunk;
?>
This is without any error handling, but that shouldn't be too much hassle as well.
Now, for the client side, there are of course some subtleties, as you will need to have two chunks in the text field at once in order to provide a "smooth" experience, but it should be doable.

Optimizing a static php website

i am making a "static" php website in this style
<?php include "header.php" ?>
<?php include "left.php" ?>
<?php include "photos.php" ?>
In "photos.php" i have 3 heavy javascript files from the lightbox and i thought it could be a good to include the javascript files only in this "photos.php" and not at the "header.php".
But javascript supposed to be only in the head html tags. Do you have any better approach or mine is just fine?
thanks very much
It's best to have all javascript in the head whenever you can. And you can without much difficulty. As Dominic Rodger said, it's probably not a big deal to include the js files on every page because they should be cached.
I tend to create page template class files with lots of variables for this sort of thing. A simpler thing to do that's more inline with what you're already doing is to set a variable before you include the header file, then access that variable in the header file and add the js if appropriate.
<?php
$includePhotoJavascript = true;
include "header.php";
?>
In the header file:
if(isset($includePhotoJavascript) and $includePhotoJavascript == true)
{
// add the javascript
}
JavaScript does not only have to be in the head HTML tag. It is actually advisable to put it at the end of the HTML file, because they halt the rest of the HTML file from loading.
What you could do in the header.php file, is something like this:
<html>
<head>
<title><?php print $title; ?></title>
<?php
foreach($javascript as $src){
?>
<script type="text/javascript" src="<?php print $src; ?>"></script>
<?php
}
?>
</head>
Then the file you posted would look like this:
<?php
$title = "Photo album";
$javascript = array("jsfile1.js", "file2.js");
include "header.php"
include "left.php"
include "photos.php"
?>
If you're going down this route, you could try setting a variable before you include header.php which stores whether or not those JavaScript files are needed. It might not be necessary to sweat it too much though, if users tend to stick around on your website they'll fetch those files once and then not again, since hopefully your server will return a 304 Not-Modified response, and they'll be served from your browser's cache.
thanks very much for your ideas.
i am planning of making the home page(index.php) as light as posible. So
i will load javascript(lightbox.js) only in photos.php and try to do a
LAZY LOADING to this lightbox.js so when "first time" visitor go to
photos.php javascript file will be in cache.
and all these in background.
I think is the same Facebook does. Look here
Optimizing Facebook

Categories

Resources