0

I have some code that displays a gallery of images. I have a call to a file that goes like this.

<script src="js/main.js"></script>

Now one part of this file contains the code below. I would like to pass an argument into the file and then replace the tags element that is currently 'cars' with the given argument. I understand that i can do something like

<script src="js/main.js">
var tag = cat;
</script>

but i have no idea how to process that argument within the javascript.

Thank you very much! This is the section of the javascript file where i want to use the argument and do something like tags = 'argument'

// Load images via flickr for demonstration purposes:
$.ajax({
url: 'http://api.flickr.com/services/rest/',
    data: {
        format: 'json',
        method: 'flickr.photos.search',
        api_key: 'XXXXXXXXXXXXXXXXXXX',
    tags: 'cars'

    },
    dataType: 'jsonp',
    jsonp: 'jsoncallback'
}).done(function (data) {
    var gallery = $('#gallery'),
        url;
    $.each(data.photos.photo, function (index, photo) {
        url = 'http://farm' + photo.farm + '.static.flickr.com/' +
            photo.server + '/' + photo.id + '_' + photo.secret;
        $('<a data-gallery="gallery"/>')
            .append($('<img>').prop('src', url + '_s.jpg'))
            .prop('href', url + '_b.jpg')
            .prop('title', photo.title)
            .appendTo(gallery);
    });
});
user2278854
  • 1
  • 1
  • 1
  • 3
  • If I understand your question right, all you have to do is tags: tag instead of tags: 'cars' – Fergus Apr 14 '13 at 04:37

1 Answers1

1

You can set your argument as window.arg=value or simple arg=value and access it as window.arg or arg provided you don't have any other arg object in the same local scope. And you set the value before the execution happens which uses this variable.

Issue with your code could be:-

<script src="js/main.js">
var tag = cat;
</script>

should be (assuming the ajax call is executed on load of the js file, of not order doesn't matter)

<script type="text/javascript">
 var tag = 'cat';
</script>

<script src="js/main.js"></script> 

//Now you will be able to access tag inside the file as tags: tag in your ajax call.

...
$.ajax({
url: 'http://api.flickr.com/services/rest/',
    data: {
        format: 'json',
        method: 'flickr.photos.search',
        api_key: 'XXXXXXXXXXXXXXXXXXX',
    tags: tag

    },
   ....
PSL
  • 123,204
  • 21
  • 253
  • 243