How can i run php script when page is fully loaded - php

I am fetching tweets and showing them into the slider . now when i open the page it shows me all the tweets one above another which create a mess. and after page fully loaded it shows me tweets as i wanted . so is there any way to show tweets or run php code when page is fully loaded ?

PHP code can not be run once the page has loaded, however you can run an AJAX call to a php script.
Although, I would suggest you can run Javascript/jQuery once the page is loaded. Your best bet is to use https://dev.twitter.com/docs/embedded-timelines
This will provide all the code you should need.

It sounds like all your tweets are output via php, and then styled into a slider with javascript, so in between the data being output and the javascript loading, you see the unstyled data.
A simple solution would be to give the containing element a style of display:none, then show it via javascript when the page has loaded:
css
#tweet-holder{
display:none;
}
php / html
<div id="tweet-holder">
//all your tweets go here
</div>
js
$(function(){
$('#tweet-holder').show();
});

Related

How do I preload .css and .js files, before the whole page (dynamic by PHP) is generated

My homepage (index.php) is generated dynamicly by PHP, so it takes about 1s till the the page is delivered to the user.
Because my css and js script is in the header of (index.php), after this 1s the css and js will start to download.
Is there some general way of preloading this js and css files?
My aproach would be to delete all content of index.php, include just <'head> with my css and js file, and then do some js to load the whole indexOriginal.php (with the dynamic content generated on my server by PHP), so this should enable the user to download the css, js in parallel of indexOriginal.php.
Is this the right aproach?
The approach you suggested could work. You also might consider altering your PHP script to hurriedly deliver the first part of the page (<html> down through <body>) before it starts getting the rest of the page together (see http://php.net/manual/en/function.flush.php).
Your page full generated by backend and sended to user only after php script end its work.
You could, for example, create page like.
<html><head><script></script></head><body></body></html>
In <script></script> load, for example, JQuery and do something like
$(document).ready($('body').load('/path/to/your/script.php'))
But I really thing this wouldn't be a good practice. Also I don't think that Google would parse your site content with this solution.
I think you should look better about your JS (compress, gzip, split to many files and upload them from different CDNs, move everything to $().ready, etc.)
My recommendation would be to have your initial PHP script JUST deliver the CSS and JS tags. Once those are loaded, use the JavaScript to make an AJAX call that gets the rest of the page then displays it. Or you can build the page in the JavaScript (which would allow you to use loading icons and the like to give a better indicator to the user that "the page is coming")

iFrame functionality with Ajax/jQuery

I need to include a small form from a reservation PHP module that I downloaded to my website. Its documentation says that it has to be included with iFrame. However the place is restricted, so the iFrame overflows when the date picker is opened and scrolling bars are shown.
To solve this, I tried using jQuery so that the div would be overflowed.
<div id="siteloader">
<script>
$(function(){
$('#siteloader').load('<?php echo base_url('booking/form_homepage.php')?>');
});
</script>
</div>
However, it results that it loads only the HTML. This is not necessary for me, since the page I want to load comes from a complete module, so I need it to be loaded complete, basically what the iFrame would do.
Is there any way to achieve this with Ajax/jQuery?
Thanks in advance

Best practive to call PHP+jQuery pages with jQuery

jQuery function:
function refresh(ppp){
$("#content").load("process.php"+ppp)
}
PHP process.php:
$vID = $_REQUEST[id];
include page_$vID.php;
The problem is that any code like datatables, jquery stuff doesn't load in the new content. I have to include all .js and .css in this piece of script. And some of the jQuery plugins doesnt work anymore.
What are the best prectices in this case? How do you integrate PHP+jQuery and jQuery's Load()?
lookup $.ajax() might do what you want. http://api.jquery.com/jQuery.ajax/
It's hard to say with the little data you have provided, but my guess is that you are trying to add stuff to the interface that requires some sort of initialization by javascript.
For instance, if you have some content like this:
<input class="datepicker" />
And a plugin that adds a datepicker widget to this input, the widget does not work on dynamically loaded content. This is most likely because you have some code in $(document).ready() that initializes your widgets. This code only runs the first time the page loads however, and not after you load some new dynamically loaded content.
To solve you would have to run the same initialization code found in your document ready code, and run it again after your dynamically loaded content is loaded.
Other than that, I believe JQuery automatically tries to load scripts found in dynamically loaded content, so I'm not sure why that is an issue. Would be helpful to see the actual page content that you are trying to load.
as for dynamically loaded css, see this link: How to apply inline and/or external CSS loaded dynamically with jQuery

Delay for Simple HTML DOM Class

I'm using the simple html dom class and have gotten it to work on basic pages and can view the information I want. However, when I attempt to use it on a page that reloads a div with ajax, I can't seem to get it to "wait" before reading the page.
I basically want it to load the page, then wait 2 seconds before reading the page content (so that the new div has time to load). Is this possible or am I trying to use the class incorrectly? I'm manually inputting the URL, so it's not a link issue.
Example Page:
- You can see the load issue when you navigate through the pages.
Someone suggested curl and I tried that with the same results.
Thanks in advance.
PHP runs on the server. JavaScript (e.g. AJAX) runs in the browser, after the PHP code on the server has finished producing the page. You can't make a PHP program, running on the server, wait for an event that happens later in the browser.
You'll need to either load the content for that div using PHP code, or replace the PHP DOM-parsing code with JavaScript code that does the work on the client.
You can use the Sleep method ( http://php.net/manual/en/function.sleep.php ) if you simply want to delay the program execution for some set amount of time.

PHP: how to determine if the browser supports javascript in PHP?

I am building an AJAX deep-linked site.
I want PHP to load all the HTML code of the page if the user is trying to access the site with a Javascript non-supported browser or if it is a search crawler. Basically PHP will return the whole page.
On the contrary, when the user is trying to access the site with Javascript supported browser, I want PHP to return only the template code, and let Javascript (AJAX) take care of the rest. Basically PHP will only load design elements and let Javascript populate them with content.
I looked into PHP's get_browser() function, however it seems it is not such a reliable tool. What is the industry's practice see if the browser supports Javascript or it is a search crawler using PHP?
Background:
Why I want the site to have this behavior.
Since I want the home page to load just by loading the address: example.com, which does not send any query to PHP, PHP returns the HTML code of the home page. This however causes issues when the user tries to load the following page: example.com#foo. So, for this example, PHP will return the home page and once the home page is loaded, Javascript (AJAX) will change the content around so that it shows proper content for #foo. This will make the user to see the home page, therefore load time will be slower and user-experience will not be so nice. However if my PHP script can figure out that if the use with Javascript supported browser is trying to load the page, it will only return the template of the web site, which has no content) and the javascript will populate that template with content whatever is supposed to be displayed for #foo. On the other hand, if the Javascript non-separated browser or a crawler will try to access the page example.com#foo, home page will be returned.
I am using SWFaddress (http://www.asual.com/swfaddress/) library for the deep-linking.
Edit
Thank you guys. I did not think of using <noscript></noscript> before.
Here is what I decided to do. PHP by default will load pages such as example.com or example.com#foo (which is essentially the same as example.com from PHP's point of view since fragments by definition are not sent to the server) blank (just visual template) with <noscript> tag inside for the content of the home page. This way users with javascript will not see the home page and AJAX will populate the content of the page according to the #foo fragment. On the other hand, search crawlers and users without javascript will see a home page.
Thank you again. I think this is pretty simple and elegant solution. If you have any further suggestions, please post a comment or another answer.
You can't do this using PHP. What you can do though is use a noscript tag to redirect to another php page if they don't have javascript:
<noscript>
<meta http-equiv="refresh" content="0; URL=nojavascript.php">
</noscript>
It's not possible to accomplish this in the way you're trying to do it.
It's rare that someone has JS turned off and doesn't know it.
PHP doesn't get passed anything after #, only javascript can do anything with that. So even if PHP could determine if the browser has javascript turned on then it still couldn't read # anyways.
You could include a link inside some <NOSCRIPT> tags that point the user to something like example.com#foo?javascript=disabled.
Unfortunately, browsers do not report whether JS is enabled or not, so there's no way to know from a simple HTTP GET whether or not you should send JS reliant pages.
You should just build an AJAX query that sets a session variable for javascript enabled.
Run this AJAX query before any other information on the site is loaded and then do a simple redirect to the actual site.
You could do something like this pseudo code:
Index.php:
ajax(check_js.php);
redirect(main_page.php);
check_js.php
$_SESSION['js_enable'] = true;
main_page.php
if($_SESSION['js_enable'] == true) {
//execute page
} else {
header("Location: no_js_error.php");
}
Instead of the server trying to sniff our the user's settings, how about using unobtrusive javascript in the first place? This way, the page will degrade gracefully (to the desired state) if JS is not available.

Categories