By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,568 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,568 IT Pros & Developers. It's quick & easy.

Dinamic Image from parameter in URL

P: n/a
Hello, i know nothing about Javascript and i hope someone in the bast
net can help me resolve this, i need to show an image based on a
parameter in the url.

http://www.mysite.com/modules/news/i...hp?storytopic=[value]

Where i have numeric values to say 5 max... so if storytopic=1 shows a
diferent header as if storytopic value where 2, or 3 for example.

i got this from the web....
Expand|Select|Wrap|Line Numbers
  1.  
  2. function getURLParam(strParamName){
  3. var strReturn = "";
  4. var strHref = window.location.href;
  5. if ( strHref.indexOf("?") -1 ){
  6. var strQueryString =
  7. strHref.substr(strHref.indexOf("?")).toLowerCase();
  8. var aQueryString = strQueryString.split("&");
  9. for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
  10. if (aQueryString[iParam].indexOf(strParamName + "=") -1 ){
  11. var aParam = aQueryString[iParam].split("=");
  12. strReturn = aParam[1];
  13. break;
  14. }
  15. }
  16. }
  17. return strReturn;
  18. }
  19.  
  20.  
but im still tring to figure it out... :-$

can someone be kind enough to help me with this, that -i only assume-
must be kind of easy to someone who knows the language.

Thanks.

Rojo, DJ.
México

Aug 25 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

me***********@gmail.com wrote:
Hello, i know nothing about Javascript and i hope someone in the bast
net can help me resolve this, i need to show an image based on a
parameter in the url.

http://www.mysite.com/modules/news/i...hp?storytopic=[value]

Where i have numeric values to say 5 max... so if storytopic=1 shows a
diferent header as if storytopic value where 2, or 3 for example.
If when your form gets submitted and your url looks exactly like the
above, then this will suffice:

function getStoryTopic()
{
var querystring = location.search;
var topic = -1;

if(querystring.length 0)
{
topic = +querystring.split("=")[1];
}

switch(topic)
{
case "1":
//show image for topic 1
break;
case "2":
//show image for topic 2
break;
case "3":
//show image for topic 2
break;
case "4":
//show image for topic 2
break;
case "5":
//show image for topic 2
break;
default:
//there was no topic
break;
}
}

Aug 25 '06 #2

P: n/a
*** me***********@gmail.com escribió/wrote (25 Aug 2006 15:42:19 -0700):
Hello, i know nothing about Javascript and i hope someone in the bast
net can help me resolve this, i need to show an image based on a
parameter in the url.

http://www.mysite.com/modules/news/i...hp?storytopic=[value]

Where i have numeric values to say 5 max... so if storytopic=1 shows a
diferent header as if storytopic value where 2, or 3 for example.
Untested:
<script type="text/javascript"><!--
function parseGet(variable){
var get=document.location.search.substring(1).split('& ');
for(var i=0; i<get.length; i++){
var igual=get[i].indexOf('=');
if(get[i].substring(0, igual)==variable){
return unescape(get[i].substring(igual+1));
}
}
return('');
}

var img='';
switch(parseGet('storytopic')){
case '1':
var img="foo.jpg";
break;
case '2':
var img="bar.jpg";
break:
}
if(img!=''){
document.writeln('<img src="' . img . '">');
}
//--></script>


--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Aug 25 '06 #3

P: n/a
Thanks everyone.. i tried both your scripts, but as none worked for me,
most probably because i did wrong i used this and is working.

Expand|Select|Wrap|Line Numbers
  1.  
  2. function getURLParam(strParamName){
  3. var strReturn = "";
  4. var strHref = window.location.href;
  5. if ( strHref.indexOf("?") -1 ){
  6. var strQueryString =
  7. strHref.substr(strHref.indexOf("?")).toLowerCase();
  8. var aQueryString = strQueryString.split("&");
  9. for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
  10. if (
  11. aQueryString[iParam].indexOf(strParamName + "=") -1 ){
  12. var aParam = aQueryString[iParam].split("=");
  13. strReturn = aParam[1];
  14. break;
  15. }
  16. }
  17. }
  18. return strReturn;
  19. }
  20. var topic = getURLParam("storytopic");
  21.  
  22. switch(topic)
  23. {
  24. case "1":
  25. document.writeln('<img src="image1.jpg">');
  26. break;
  27. case "2":
  28. document.writeln('<img src="image2.jpg">');
  29. break;
  30. case "3":
  31. document.writeln('<img src="image3.jpg">');
  32. break;
  33. default:
  34. document.writeln('<img src="default.jpg">');
  35. break;
  36. }
  37.  
  38.  
Very usefull to me... Thanks again for your reply's.

Gracias

Rojo, DJ.
México.

Aug 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.