Dynadot โ€” .com Transfer

Is there a way to do this without an iframe?

Spaceship Spaceship
Watch
Impact
87
I want to have a link be clicked, and an image shows up on a certain part of the page. Then when you click another link, a different pic shows up in that spot. I know this can be done with iframes but my layout gets screwed up when I use them for some reason.

So is there a different way to do it?

Thanks :)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
it can be easily done using javascript, but i dont know how. :)
 
0
•••
I hate javascript, and there's probably another way to do this, but let's go the AJAX route:

img.js
Code:
function createRequestObject()
{
  var ro;

  if (window.XMLHttpRequest)
  {
    ro = new XMLHttpRequest();
  }
  else if(window.ActiveXObject)
  {
    ro = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return ro;
}

var http = createRequestObject();

function sndReq(picfile)
{
  var str = picfile;
  http.open('get', 'image.php?img='+str);
  http.onreadystatechange = handleResponse;
  http.send(null);
}

function handleResponse()
{
  if(http.readyState == 4 || http.readyState == 0)
  {
    var response = http.responseText;
    document.getElementById('pic').innerHTML = response;
  }
}

image.php
PHP:
<?php

$img = trim(stripslashes(strip_tags($_GET['img'])));
echo '<img src="path/to/images/'.$img.'" border="0">';

?>

So, an HTML page like:
Code:
<html>
<head>
<title>Test</title>
<script language="JavaScript" type="text/javascript" src="img.js"></script>
</head>

<body>

<a href="#" onclick="sndReq('testimg.jpg');">Test</a>
<br />
<br />
<div id="pic">
</div>

</body>
</html>

And you can see it here: www.secondversion.com/ajaximg/
 
1
•••
Thanks for that 2ndV!!! I was planning on creating something similar but had no idea how to do it :). Thanks agian! I tried leaving rep but it wouldn't let me as I've left rep for you recently before, so I'll send over some NP$.

Regards, Rhett.
 
0
•••
That IS a nice script. Thanks too! I just learned AJAX in 2 minutes when over the Net it took me 10 days and I gave up on it after not learning a single yarn of it!

Rep+!
 
0
•••
Thanks SV! I'll try that, rep added!

And sorry if this is a dumb question but..is there a way to make that image that shows up a link as well?
 
Last edited:
0
•••
CriminalOrigins said:
Thanks SV! I'll try that, rep added!

And sorry if this is a dumb question but..is there a way to make that image that shows up a link as well?
No not at all :) You could make it a link:
PHP:
  <?php

$img = trim(stripslashes(strip_tags($_GET['img'])));
echo '<a href="somewhere"><img src="path/to/images/'.$img.'" border="0"></a>';

?>
 
0
•••
For image.js is there things I need to edit? Like where it says picfile should I edit that to the path to the image or just leave it the way it is?

Thanks for the help!
 
0
•••
CriminalOrigins said:
For image.js is there things I need to edit? Like where it says picfile should I edit that to the path to the image or just leave it the way it is?

Thanks for the help!
Nope, don't need to edit anything in the js, unless you change the name of the php file... what you need to do is:
PHP:
  <?php

$img = trim(stripslashes(strip_tags($_GET['img'])));
echo '<a href="somewhere"><img src="set/your/path/to/the/image/folder/here/'.$img.'" border="0"></a>';

//Change set/your/path/to/the/image/folder/here/ to your image folder ;)

?>
 
0
•••
Ok so I just need to create a folder with the images I want in it, then put the path to that folder in the PHP file, then in the HTML file type in the file name of the image where it currently says testimg.jpg?

Thanks again for your help.
 
0
•••
CriminalOrigins said:
Ok so I just need to create a folder with the images I want in it, then put the path to that folder in the PHP file, then in the HTML file type in the file name of the image where it currently says testimg.jpg?

Thanks again for your help.
Yep :)
 
0
•••
I get this error when I click on the link

'; //Change set/your/path/to/the/image/folder/here/ to your image folder ;) ?>

Obviously I didn't take out your text but the image isn't showing up either.
 
0
•••
Plain JS would be easier.

Code:
<script>
function changePic(pic)
{
  document.getElementById('picture').innerHTML = '<img src="path/to/pic/' +
pic }
function changeURLPic(url, pic)
{
  document.getElementById('picture').innerHTML = '<a href="' + url '"><img src="path/to/pic/' +
pic + ' /></a> }
</script>
<div id="picture"></div>

<a href="#" onClick="changePic('image.jpg')">change pic</a>
<a href="#" onClick="changeURLPic('http://google.com', 'image.jpg')">change the pic and make it a link</a>

EDIT: that doesn't work (I'm not too good at JS), but it's something like that.
 
Last edited:
0
•••
Dynadot โ€” .com TransferDynadot โ€” .com Transfer
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back