Is it possible to open a window from PHP that has predefined content? It's obvious how you can open a window from a javascript link that frames an existing page, or just do a target=_blank from a regular a tag that references an existing page. But I am generating a bit of content, and want that content to be opened in a new link (or streamed to the viewer)--
something like (clearly psuedo code!):
$content = "Hello World. <br />Nice to meet you!";
<a href="#" target="_blank" content=$content>Open up!</a>
Is this possible?
Thanks!
Well, the direct answer to your question is that you can't really do it from PHP directly, because it's the browser that's going to open the window. You can, however, have your page open a window, get the document object, and write to it:
var w = window.open("Surprise", "#");
var d = w.document.open();
d.write("<!DOCTYPE html><html><body>Hello World</body></html>");
d.close();
Instead of the simple string "Hello World" of course your PHP script can put together whatever it wants. Additionally, if desired, the Javascript code itself can dynamically generate the content based on page status, form fields, etc.
Note that you can't guarantee that the new window won't be a new tab, which is no different than what happens with "target" in <a> or <form> tags.
edit — oh also: if you try to use window.open outside of code that's running in response to a "click", browsers will probably think you're trying to show a pop-up ad and will block it.
Related
I would like to download content of certain page and get one number from it (still not sure how, probably using PHP DOM interface). I opened the page, started Firefox's debugging, picked the element with number and found out that is in <div id="lblOptimizePercent" class="wod-dpsval">98.4%</div> (98.4% is what I am looking for). So I opened its source code, Ctrl - F for lblOptimizePercent and all I found is this <div id="lblOptimizePercent" class="wod-dpsval"></div> without any content. What I've done wrong? Or is it some site's protection not to steal contents?
Link to the original site
Normally, to scrape the page from PHP, you would have to
save the page
extract the value you want from HTML via a regular expression
alternatives include using SimpleXML for DOM querying...
The piece of HTML we are look at is:
<div id="lblOptimizePercent" class="wod-dpsval">DATA</div>
<?php
$text = file_get_contents('http://www.askmrrobot.com/wow/optimize/eu/drak%27thul/Ecclesiastic');
$regexp = '^<div id=\"lblOptimizePercent\" class=\"wod-dpsval\">(.*)<\/div>^';
preg_match($regexp, $text, $matches);
$percentage = $matches[1];
echo $percentage;
This should give you DATA - the percentage value. But this doesn't happen! Why:
The data is dynamically inserted by a Javascript on client-side.
The id or class selector is used for DOM querying (element selection), then the data value is added.
http://api.jquery.com/id-selector/ - http://api.jquery.com/class-selector/
jQuery example
On this site they deliver <div id="lblOptimizePercent" class="wod-dpsval"></div>to the client and then they use an update query like this: $("#lblOptimizePercent").text("100%"); to update the percentage value.
If you want to query it on client-side, you might use $("#lblOptimizePercent").text();**
Try this in your console. It returns the percentage value.
How to scrape this page?
If you want to scrape this page with dynamic data, you need something like a Browser Environment for scraping: PhantomJS or SlimerJS are your friend.
Open the page with PhantomJS, launch the jQuery cmd from above and done.
This snippet should get you pretty close. You might save it as scrape.js then execute it with Phantom.
var page = require('webpage').create();
page.open('http://www.askmrrobot.com/wow/optimize/eu/drak%27thul/Ecclesiastic', function() {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
alert(
$("#lblOptimizePercent").text()
);
});
phantom.exit()
});
});
You can also save the "evaluated page" (now with data) and do the extract with PHP.
That's exactly like: Save Page in your browser and working on the saved HTML file.
In Firebug or another webdeveloper tools you see the generated content, in Source code there is a blank element only.
First time, blank element is shown (during rendering site) and than using JS the content is filled.
Googlebot etc. can´t see this JS-generated content, but it´s no problem in this case.
Code:
document.getElementById('lblOptimizePercent').innerHTML = '94%';
Or similarly using jQuery:
$('#lblOptimizePercent').html('94%');
// need to load jQuery before, of course
I know that there are at least two other similar questions, but they do not help me.
I have textarea with tinymce where user writes his text. Then there is a button "PDF" that should create pdf and open it in new tab. The content of pdf is the content in tinymce.
When user click on button, the form is submitted to index.php action. Then index.php gathers information from $_POST variable and creates pdf.
I cannot get it to open in new tab as a normal link.
I tried it in different ways.
1) I can open PDF in the same tab, but this is not what I need. This line opens my pdf on the same tab:
$dompdf->stream('document.pdf',array('Attachment'=>0));
2) I can open it in new window, but then browser warns that this is pop-up. Client doesn't want it. Also another problem with this is that pdf is stored on server. I do not want it (pop-up warning is more important). Here is my code:
$output = $dompdf->output();
file_put_contents('document.pdf', $output); //save pdf on server
//opens generated pdf in new window, but this creates warning for popup
echo '<script type="text/javascript" language="javascript">
window.open("http://modeles-de-lettres.org/test/saved_pdf.pdf", "_blank");
</script>';
I have read those:
1) Open PDF in a new tab using dompdf
This suggests: "As far as opening in a new tab. That depends on how you are generating the PDF. But the simplest way it to provide a link with a target attribute." I think this means that I have
<a href="my.pdf" target="blank"
or
<a href="my.php" target="blank"
But this does not work for me, because I should POST my form to get data from pdf.
2) generate the pdf on newtab in dompdf
This is something that I have implemented before (I did it without sessions), but it creats warning about popups.
You can simply do the following:
<button type="submit" formtarget="_blank">Submit to a new window</button>
formtarget="_blank" attribute will open new tab.
Hope it will help you!
The problem is the order in which you're doing things. You should be opening the new window from a user event, like clicking a button. If a script on a page load event tries to open a window, the browser will presume it's an unwanted popup, since the user hasn't done anything besides navigating to the page.
What could work for you, is adding a target="somewhere_new" attribute to your form tag. This way, the browser would open a new tab since there's no frame/iframe with a name="somewhere_new" attribute, and you wouldn't have to do anything special in the server side, just process the data as you did before using $dompdf->stream('document.pdf',array('Attachment'=>0)); at the end
I am doing file upload async with jquery/php using the target attribute of a form. The target is my iframe named "uploads"; To manage error in the php file, I return string like "ERR_TOO_LARGE_FILE" with an echo which will be inside the iframe body.
The problem is I don't know how to detect when the iframe is ready and access its content to see if there's an error. I tried the following code but it didn't work:
$("#uploads").on("load", function() {
alert($(this).html());
});
an alert box did appear, but nothing inside. I also tried using the ready event instead but this time, nothing appears.
Would like to know a way to achieve this or an alternate solution if you have a better one.
Because you're not allowed to modify the content of an iframe, at least you're not suppose to, you can't directly access it using jquery. Use .contents() & .find() to get the html from the iframe so you can get the info you need.
So,
$("#uploads").on("load", function() {
alert($(this).contents().find('html').html());
});
should return the contents of the html in the iframe. You can swap out the 'html' for a tag id if you know the specific element you're looking for.
I have an overlay popup box (DIV + JavaScript + some CSS) which is toggled by a link, like this:
<a href="#" onclick="popup('popUpDiv')">
The box that pops up contains an iframe. Now I want different links on my page to load different iframes when the box opens; so that link A would toggle the box to appear, and load iframe1.html inside the box, while link B would toggle the same box to appear, but load iframe2.html inside the box, and so forth.
I'm a beginning programmer, and I first figured maybe this could be done with PHP, by putting something like this inside the popUpDiv:
<iframe src="http://mysite.com/iframe<?php echo $_GET ["iframeid"] ?>.html">
... and then simply adding ?iframeid=x to each link's href, where x is the id of the iframe to be loaded.
This obviously won't work in practice though, because it requires a page reload, and when the link is clicked, the popUpDiv is toggled to open, but at the same instant, the page reloads, now with the ?iframeid=x query string in place, but too late, since the popUpDiv disappeared on reload.
Is there perhaps a JavaScript equivalent that could be used, or can anybody suggest another way to make this work?
FYI, this is the kind of popup box I'm working with:
http://www.pat-burt.com/csspopup.html
I finally found a really simple way of accomplishing this without additional JavaScripting, by simply using the target attribute:
<iframe name="myfavoriteiframe">
Then:
This is a link.
P.S. Just FYI for any of those out there who might want to use this feature with Vimeo's JavaScript API, as I'm doing: In order to make API buttons work with this target method, it seems like the iframe src-attribute cannot be left blank. You need to specify a link to any of your videos in the src, so that a video loads into the iframe on page load, like this:
<iframe name="myfavoriteiframe" id="player_1" src="http://player.vimeo.com/video/[the_number_of_your_video]?api=1&player_id=player_1">
I simply inserted the URL to the first video on my page, but you can use any of them, since they're hidden anyway.
Then link to any of your videos using the target method described above, and the pre-set iframe video will be replaced by a video of your choice.
Create two divs with the two variations of content (the two iframes with each specific URL). Then toggle by the name of the div.
<a href="#" onclick="popup('popUpDiv1')">
<a href="#" onclick="popup('popUpDiv2')">
Set the id attributes of the div elements appropriately.
Rather than adding something to the popup DIV element, I would add a required link to the href property of the link that you use to open the popup:
<a href="http://example.com/first/uri" onclick="pre_popup('popUpDiv')">
<a href="http://example.com/first/uri" onclick="pre_popup('popUpDiv')">
<a href="http://example.com/first/uri" onclick="pre_popup('popUpDiv')">
And then inside the popup function I would take the href parameter of the link that has triggered the event and use it in a popup:
function pre_popup(id) {
// Take the href parameter of the link
var href = this.href;
// And then use it in the iframe
window.frames["your_iframe"].src = href;
// And then pop it
popup(id);
}
I was wondering if this was possible to do. I know you can pull a html file and put it on your page like this, <a href='index.php?content=Contact.html'> . Is there someway to pull a webpage from a URL to your site. So instead of a link open in another tab, it would open that webpage on your current site page. If it's not possible, is there some sort of similar solution I can use.
You can use the iframe tag to display other web pages inside your own web page.
When you say:
I know you can pull a html file and put it on your page like this, <a href='index.php?content=Contact.html'>
Actually, that's not a normal feature - it's only a function of whatever index.php file is on your server, and simply displaying content referenced by a GET parameter can actually quite dangerous depending on whether you know what to protect against and how.
I guess it depends on how "embedded" in to your site you wish for it to be, but it sounds like you could use iframes.
If you want to load an external website into your site, have a look at iframes [docs].
If you want to update parts of the page with content coming from your domain and without refreshing the whole page, you can use Ajax.
Yes, it is possible. You can use AJAX to get the other file and then set the .innerHTML property of your div to the loaded content. In the simplest way, with jQuery you'll have something like this:
var data = jQuery.get("http://my.domain.com/file.html")
$('#mydiv').html(data);
you could use jquery's load() function. i would do it something like this:
$(document).ready(function () {
$('a').click(function (e) {
e.preventDefault();
$('#myDiv').load($(this).attr('href'));
}
}