Jump to content

[SOLVED] A php file forced through an <img> to fire off javascript function?


thecard

Recommended Posts

I have an <img src="file.php" />

So this php file sets it's headers to look like an image and displays an image.

But would it be possible for this php file to also carry out some javascript as well?

I have tried echoing a link to a javascript file like this inside file.php:

echo"<script src='js.js'></script>";

But I can see why this doesn't work..I think because once the headers are set inside the php file to be an image, it can't echo anything, and so can't even echo a link to javascript!

How can I make a php file, "fired" through an <img> tag set off javascript functions from within the php file?  ???

Thanks for any help.

thecard

Link to comment
Share on other sites

Thanks, I'll try that when I get home.

So I must rename the .js file to .php?

Why might this make it work?

I thought the reason it didn't was because I couldn't echo anything inside the .php file because it had img headers.

As I said, I'll try it tonight.

 

thecard

Link to comment
Share on other sites

Thanks, I'll try that when I get home.

So I must rename the .js file to .php?

Why might this make it work?

I thought the reason it didn't was because I couldn't echo anything inside the .php file because it had img headers.

As I said, I'll try it tonight.

 

thecard

 

You could link java.textfile  as long as it prints out actual javascript. You are telling the tag <script that the file should be type text/javascript, or you should I guess:

echo "<script type='text/javascript' src='js.php'></script>";

 

Although it is not needed, best practice. The above is common practice cause sometimes you want to dynamically create a javascript with custom data etc from your script, so the above would allow you to use php to gather data then print out javascript and have it work all hunky dory. Where as the .js does not allow for this functionality, that means the file will not change unless you manually change it.

 

EDIT:

I just read the original post more closely, sorry you were asking if you can run the javascript inside of an image file, I do not think this is possible due to the fact the image is a binary type and adding txt to it will kill it/make it not display.

 

So the short answer would be no.

 

Link to comment
Share on other sites

The javascript will carry out AJAX -> php mysql querys which must be asynchronous :(

 

What would this javascript send back to the server? What I am getting at, is why does it need to be JS in the image? What data are you trying to gather from it, and why can you not just put the query update inside the file.php?

Link to comment
Share on other sites

Right:

The img tag links to a .php file which loads an image after some mysql requests to make sure which one to load etc.

I want the php file to send off this information (the timestamp etc. and more complex stuff) asynchornously to my database, so the best way to do this would be through javascript I think.

But how can I link the javascript file with the xmlhttprequest and handler function to the .php file being used as the image?

 

Thanks for your hard work.

I assure you I have thought about this for a while without any results! 

Link to comment
Share on other sites

Asynchrounosly?

 

You do not need asyncrounosly.

 

When the file.php is called do your DB calls before the image is display, then print out the image. No need for js to do that, it will do it on the backend before the page is loaded.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.