I would like to create a php file (lets call it master.php) that executes automaticlly a series of scripts in a new tab. Every script listed should be executed in a different tab. I tried using header(location), however it can only be used to redirect once. Any idea?
What you want goes beyond the scope of php. You have to use javascript to control the logic among the browser's tabs you want to launch.
From php, you should generate the proper javascript code to do that, in particular take a look at the javascript function window.open
You can create a hyperlink as below:
<body onload="document.getElementById('runScripts').click()">
<a id="runScripts" href="script1.php" target="_blank" onclick="window.open('script2.php');window.open('script3.php')">Click Here</a>
If master.php returns the first page, and then you could use javascript to open the other pages. Please note that this does not have 100% browser support.
<html>
<head>
<script type="text/javascript">
window.open('page2.php','_newtab1');
window.open('page3.php','_newtab2');
window.open('page4.php','_newtab3');
window.open('page5.php','_newtab4');
</script>
</head>
<body>content of first page</body>
</html>
You could also use '_blank' instead of ''newtab'
I would suggest using Javascript to execute the scripts in different tab. However this open new windows and popup option has to be disabled. New tabs are not opened unless user explicitly clicks on an anchor element. The following code is an example from withing a PHP for loop.
window.open("script" + <?php echo$i ;?>+ ".php", '_blank');
Related
Ok so I am tasked to work with this third party vendor that shows movie times on their end. We have the chance to co-brand our site on their end, so in order to do that we need to create a wrapper of our site so that the movie times can show inside the wrapper so it still looks like your on our site. One option is to have a header and footer included via js external file, that way we can make changes on our side and the updates will show on their side, so we dont have to send them a new header/footer every time we make updates to the wrapper. They suggested we do it this way:
<script src="http://oursite.com/header.php"></script>
<script src="http://oursite.com/footer.php"></script>
So I have raw html in header.php hosted on our side and then I added this in the header before anything else:
<?php header("Content-type: text/javascript"); ?>
However when the page loads I get this error:
SyntaxError: syntax error
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xml
Im assuming there is a problem having raw html code in a js file? If so is there anyway around this?
You should add a link to proper javascript that push desired html to the page.
<script src="http://oursite.com/[somejsfile].js"></script>
You can get help from this answer on how to insert html elements using javascript
If you are going to use javascript to call an external file you need to use ajax. The problem then lies that you are taking about your site and that cannot be done when your domains do not match.
If you do it in the manner you have included you must declare the html in javascript variables then, on load push the data to where it needs to go.
var html = '<html> YOUR CODE WATCH OUT FOR SINGLE QUOTES </html>';
//check for load with jquery
$(document).ready(function(){
$('#header").html(html);
})
My issue is following: My Partner's have websites and I want that partners can include a script from my server that dynamically generates a link and an image.
i.e.
myscript.php (on my server) generates following output:
<a href="http://www.myserver.com/apples.php?12345">
<img src="http://www.myserver.com/images/12345.png" />
</a>
I don't want my partners to be bothered with any php scripting, hence the best solution for now is to supply them with an iframe, where the src is the link to my script on my server that generates the output above.
However I clearly don't have any control over my partner's website and i.e. opening a lightbox out of the iframe certainly won't work.
Because of that I tried a different approach. I used Ajax to dynamically load my script, however this also doesn't work due to ajax security features (you cannot call an external script via ajax - only if you do a php workaround).
So basically, my question is: Is there any good solution to call my script on my server on the partner's website and display the generated code on my partner's website without the use of php?
Have your PHP generate JavaScript (don't forget the content-type header). Then they can include it with a <script> element.
Make a PHP file, like
<?php
$url = "http://www.myserver.com/apples.php?12345";
$img = "http://www.myserver.com/images/12345.png";
echo "document.getElementById('divthing').innerHTML = '<img src=\"" . $img . "\" /> '";
?>
Your partner's page would be like:
<html>
<body>
Hey, check this site out: <div id="divthing"></div>
<script type="text/javascript" src="http://yoursite.com/script.php"></script>
</body>
</html>
(I know, not really clean code (innerHTML etc), but you should get the idea :))
Could you make it with javascript file which replace/creates that anchor where ever you put the javascript link.
picture.js:
$('#image').ready(function(){
var image = 'http://host/url/to/image.jpg';
$('#image').load(image);
});
on you partners site:
<div id="image">
<script type="text/javascript" src="http://yourhost/picture.js"></script>
</div>
I don't know if it possible, but.. :) and this needs jQuery. And im slow.
I'm learning all this web programming stuff after years writing .EXE Windows programs so bear with me.
I developed a basic .php and mysql website that works fine.
But I went to add javascript code to my index.php and I don't think the javascript code is executing.
My index.php has the form:
<?php
require_once blah blah
call_my_php_functionBlah();
?>
Then I added this code inside the php blocks of the '<\?\php' and "\?>" as follows:
<script type="text/javascript">
// some known-good javascript code that displays an image
</script>
Nothing showed up.
So I thought "ah-HAH, I blew it, all I need to do is -- move the javascript code outside
of the php block, at the bottom of index.php, and surely I'm good to go."
And still, Nothing showed up.
I checked the source of my 'known-good' javascript code and it said 'embed this javascript code
in your HTML file' so I thought "wow, I guess I need an index.html or something here."
So my questions:
1) should my index.php be able to run the javascript block of code?
I'm guessing 'No because index.php executes on the server and javascript runs on the client machine.'
2) How should I architect this if I want to keep my index.php, whose code works fine and I don't want to mess with it?
I'm thinking that this is an extremely basic client/server, php and javascript script organization issue that every web programmer knows how to handle, but like I said, I'm new to all this. I read in the archives about .htaccess etc. etc. but I
bet there's an easier way, and I'm not sure if the stuff I read applies.
the file name extension is completely irrelevant
PHP executes on the server and doesn't care at all about any Javascript
code inside <?php ?> tags must of course be valid PHP code to be executed by PHP
your browser receives whatever the result of your PHP execution is
you can use PHP code to output Javascript or simply have Javascript on the same page outside of <?php ?> tags
only whatever the browser receives matters, so use View Source
look at the browser's Javascript Console to debug client-side Javascript problems
Then I added this code inside the php blocks of the '" as follows:
Dont add your script inside the php block bring it outside php block.
After you are done with script you can reopen php block and write php again
index.php can run javascript, just that You need to echo the javascript code to put it in the page.
Anything that appears inside your php open/close tags has to be echoed or printed to be rendered to the html page. Anything outside your php open/close tags should appear in your html page but whether it works correctly or not is another matter not necessarily related to your php. The php interpreter doesn't run your javascript code, however, so it can't just sit inside your php tags.
Javascript will run inside .php file.
But you have to write outside the tags.
Eg:
index.php
<?php
echo "Helloooooo";
?>
<script>
function TestingMyFirstScript()
{
alert(1)
}
</script>
Javascript will execute in a PHP file but not inside of a PHP block. It executes in the server, yes and anything coming from PHP should be printed out to see. You should have the JS code outside of the PHP block and it can be anywhere in the page e.g.
It depends how to mix/match the code but of course keep it clean and easy to read (and debug).
<?php
// code here
?>
<script type="text/javascript">
// JS here
</script>
<?php
// some more code here
?>
Answer to both of your question is that you dont have to create a separate html file to execute your JS code. You can have HTML, JS, and PHP code in the same file. PHP code inside the PHP tags will be processed on the server and replace with HTML. The server generated HTML will be combined with other HTML present on the .php file and sent to the browser as one HTML.
There must be some error in the JS code which is causing the script to fail.
<?php
require_once blah blah
call_my_php_functionBlah();
?>
<script type="text/javascript">
// some known-good javascript code that displays an image
</script>
<?php
// other php code
?>
Most of the above comments should help you with your PHP + JS problem. However, if you are still getting errors with your output, try using:
alert("breakpoint 1");
//some code
alert("breakpoint 2");
throughout your Javascript function (it will show you where the code is failing). Good for beginners debugging. Also check out http://www.jslint.com/
I had one page in Views in CakePHP, it have normal javascript block,
Just inserted:
<script language="JavaScript" type="text/javascript">
---code---
</script>
Inside page, and it was all working okay...
But now... It doesn't show...
How can I change configuration or something to enable showing javascript blocks without CakePHP commands.
Javascript needs data from that page so I can't use outer file,
and it's too long to use $javascript->codeBlock
Is there any way to reconfigure stupid CakePHP to start showing those blocks?
Some files are showing javascript, and it's working all okay, but some of them won't show...
Please help...
If you mean that you want to view the code when the page is displayed, try surrounding it with <pre>...</pre>
If you mean you want the browser to process the code, then provided you are
actually going to that view file and
the code isn't commented out (<!-- ... --> or <?php /* ?> ... <?php */ ?> etc.) and
the code isn't being obliviated by a php conditional (if ... then ... else... endif)
then it will be there. Try Firefox ctrl-u to view the source.
Also try posting the view code here so that we can give you some sort of informed solution.
I've searched the entire web for a clear answer but haven't found one yet.
I have this site:
www.helixagent.com/todo/
and I want to load that entire site into a portlet in my main index page.
I tried an iFrame approach but it fails to load the site correctly. The styles and javascript files don't get loaded (i think)
Is there a way to make a portal that mimics a new browser page and load the content exactly as it would if it was directly accessed via the url bar?
It’s possible to perform cross-domain requests using jQuery.
Simply include this file after loading jQuery, and then you can do funky stuff like:
$('html').load('http://helixagent.com/todo/');
Note that this of course won’t load the embedded CSS and JS files. You’ll need to use an iframe for that:
$(function() {
$('body').append('<iframe src="http://www.helixagent.com/todo/" />');
});
Live example: http://jsbin.com/uruka
Here's an example using jQuery and iframe:
$(document).ready(function(){
$('#somediv').html("<iframe src='http://www.helixagent.com/todo/'></iframe>");
});
This should be placed inside the <head> tag of your page and it will load the entire site with all the js and css too into a div.
Have you looked at using Server Side Includes.