Forum Moderators: open

Message Too Old, No Replies

Simple HTTP query variable.

         

JAB Creations

5:15 pm on Jan 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need a little guidance creating a variable with a path that includes an HTTP query. When I pass something though a function like something(this) what is this technically called? Also how do I use it to properly construct the URL?

- John

Page Function

contact_artist(law);

Current Attempt

function contact_artist(artist) {
var ajax_this = 'templates/contact-email.php?nameis=' + (artist);
ajaxpage(ajax_this, 'promptsajax');
}

Currently Working / Static

function contact_artist_static_name {
ajaxpage('templates/contact-email.php?nameis=static_name', 'promptsajax');
}

coopster

5:38 pm on Jan 2, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It is commonly called a function argument (or parameter). In this case, you are merely making a string so build the string as you normally would ...
var ajax_this = 'templates/contact-email.php?nameis=' + artist;

In this case, it looks like you intend to use it in a GET request so you may want to be certain you url encode the variable value before concatenating it to the rest of your string.

JAB Creations

12:46 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In Firefox I get this error...
Error: law is not defined
Source file: javascript: contact_artist(law);

Using...

var ajax_this = 'templates/contact-email.php?nameis=' + artist;
ajaxpage(ajax_this, 'promptsajax');

I think ajaxpage(ajax_this might be triggering this? The parameter artist is equal to 'law' in my example function, so?

- John

coopster

2:18 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I assume that means that the
law
variable is not defined before you are attempting to use it.

JAB Creations

2:42 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function contact_artist(artist) {

'law' is defined as a(n artist)parameter, I guess this does not make it a string?

- John

coopster

2:51 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I mean prior to the function construct/definition. The variable is likely not defined prior to passing it to the function. For example ...
var someVar = false; 
function contact_artist(artist) {
// function stuff here
}
contact_artist(law); // <-- law has not yet been defined ... error!

JAB Creations

5:11 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Quotes!

javascript: contact_artist('law');

It works now. :)

What is the difference if there are or aren't quotes used?

- John

coopster

5:48 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The addition of quotes make it a literal string. Without the quotes, it is assumed to be a variable, constant or at least something other than a literal string:

law is not defined

JAB Creations

6:08 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm so going to write a book one day, thanks!

- John