1

Appreciate some insight given the following code:

<script type="text/javascript">
$(document).ready(function() {
    var soapEnv =
        "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
            <soapenv:Body> \
                 <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                    <listName>video_player</listName> \
                    <viewFields> \
                        <ViewFields> \
                           <FieldRef Name='Title' /> \
                       </ViewFields> \
                    </viewFields> \
                </GetListItems> \
            </soapenv:Body> \
        </soapenv:Envelope>";

    $.ajax({
        url: "_vti_bin/lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: processResult,
        contentType: "text/xml; charset=\"utf-8\""
    });
});

function processResult(xData, status) {
    $(xData.responseXML).find("z\\:row").each(function() {
        var liHtml = "<a href='" + $(this).attr("ows_Title") + "'><img src='" + $(this).attr("ows_snapshot_file_location") + "' width=120 height=90><strong>" + $(this).attr("ows_video_description") + "</strong><br /><br />" + $(this).attr("ows_video_description") + "<em>" + $(this).attr("ows_video_length") + "</em></a><br />";
        //{missing code}//
    });
}
</script>

What code should I enter inside the //{missing code}// such that I can store the variable 'lihtml' into a array that can be used in any javascript? (I wish to use document.write() to display the "lihtml" variable.

UPDATE 1: After taking minus4 consideration, the code is as follows:

<script type="text/javascript">
var glob;

$(document).ready(function() {
    var soapEnv =
        "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
            <soapenv:Body> \
                 <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                    <listName>video_player</listName> \
                    <viewFields> \
                        <ViewFields> \
                           <FieldRef Name='Title' /> \
                       </ViewFields> \
                    </viewFields> \
                </GetListItems> \
            </soapenv:Body> \
        </soapenv:Envelope>";

    $.ajax({
        url: "_vti_bin/lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: processResult,
        contentType: "text/xml; charset=\"utf-8\""
    });
});

function processResult(xData, status) {
    $(xData.responseXML).find("z\\:row").each(function() {
        var liHtml = "<a href='" + $(this).attr("ows_Title") + "'><img src='" + $(this).attr("ows_snapshot_file_location") + "' width=120 height=90><strong>" + $(this).attr("ows_video_description") + "</strong><br /><br />" + $(this).attr("ows_video_description") + "<em>" + $(this).attr("ows_video_length") + "</em></a><br />";
        glob.append(liHtml);
    });
}
</script>

and at the other part of the same document, I used

<script type="text/javascript">
document.write(glob);
</script>

and now, I receive the 'undefined' error. Any insight?

Larry Morries
  • 669
  • 7
  • 17
  • 1
    Since jQuery *is* JavaScript, replacing that with `document.write(lihtml)` will do. Otherwise, please clarify what you mean by 'another JavaScript'. Is it another (external?) file, another user session, an userscript? – Yi Jiang Sep 30 '11 at 08:36
  • @YiJiang, The another Javascript that I am referring to was another probably at the other part of the same file. – Larry Morries Sep 30 '11 at 08:38
  • `lihtml` will only be in scope for the `processResult` function. If you want to use the variable outside of the function you'd need to declare it differently. See - http://stackoverflow.com/questions/500431/javascript-variable-scope – ipr101 Sep 30 '11 at 08:46

3 Answers3

0

I think you should directly call any other javascript fonction from processResult function so you don't have to store data anymore. So missing code could be something like

printMe(liHtml,'SomePlace');

Or directly add the document.write function there?

document.write(liHtml);

Or maybe can you give more details about the kind of js you want to call?

Mika Andrianarijaona
  • 1,619
  • 17
  • 29
  • Please see my comments for YiJiang. Thanks. – Larry Morries Sep 30 '11 at 08:42
  • OK.So as they have already said, what you need is to declare a global variable so you need to put `var liHtml` before the `$(document).ready(...)` and just use `liHtml = ....` in the ` processResult` function – Mika Andrianarijaona Sep 30 '11 at 09:00
  • Yes, I put a global variable but it return me a 'undefined' error message. – Larry Morries Sep 30 '11 at 09:01
  • Maybe you call the other script before this one?It must be called after. – Mika Andrianarijaona Sep 30 '11 at 09:04
  • Yes, I call all other script before this one cause I know that the variable need to contain data before it can be displayed by this one. – Larry Morries Sep 30 '11 at 09:05
  • So I think the reason you have undefined is just because the ajax is not yet complete before the script to display the variable is called. For example if you try to declare glob as `var glob = "Click"` you won't have the undefined anymore. That's why I say the best solution is to create a function called ONLY when the AJAX data are ready – Mika Andrianarijaona Sep 30 '11 at 09:14
  • I don't get it. The function is called when completed `complete: processResult,` inside the `$(document).ready(function()`. Have I coded wrongly? – Larry Morries Oct 04 '11 at 01:13
0

you could create a global variable.

or if you wan to caryy this around multiple pages then try http://blog.reybango.com/2010/04/08/alternative-client-side-storage-using-sessvars-js/ it will store the data for you wihin the browser, across pages etc

then using jquery you can say $('#idofdivorspan').html('');

davethecoder
  • 3,856
  • 4
  • 35
  • 66
0

Try -

var glob = [];

then -

function processResult(xData, status) {
    $(xData.responseXML).find("z\\:row").each(function() {
        var liHtml = "<a href='" + $(this).attr("ows_Title") + "'><img src='" + $(this).attr("ows_snapshot_file_location") + "' width=120 height=90><strong>" + $(this).attr("ows_video_description") + "</strong><br /><br />" + $(this).attr("ows_video_description") + "<em>" + $(this).attr("ows_video_length") + "</em></a><br />";
        glob.push(liHtml);
    });
}

and for output -

document.write(glob.join(""));

That should build up a dynamic array of your html strings, that can be output via the array.join method. I think the problem with you original code is you're using `append' on a normal JavaScript variable when it is a jQuery specific function. Adding the content to a dynamic array should have the same effect.

ipr101
  • 24,096
  • 8
  • 59
  • 61
  • Tried but this time, there is no error message, just a blank screen. – Larry Morries Sep 30 '11 at 09:08
  • If you add `console.log(liHtml)` after the `glob.push(liHtml)` line are you getting any data returned? – ipr101 Sep 30 '11 at 09:13
  • Still the same thing - a blank screen. Am I missing something? – Larry Morries Sep 30 '11 at 09:16
  • 1
    You need to look in the console of your browser - I suspect you may be getting no data returned. You could also try `alert(liHtml)` and see if you get any alert messages. – ipr101 Sep 30 '11 at 09:20
  • +1 no alert message. can tell me how to get that data returned? – Larry Morries Sep 30 '11 at 09:21
  • You're either getting no data back at all or the function that loops through your data is not working. You could `alert(xData.responseXML)` to see what you're getting back. Although you'd be best logging to the console as that would allow you to explore any objects returned. – ipr101 Sep 30 '11 at 09:28
  • there is nothing shown too. By the way. the `alert(xData.responseXML)` is it put inside the `function processResult(xData, status)` or put where? – Larry Morries Oct 03 '11 at 00:43
  • `alert(xData.responseXML)` should go inside `processResult` if `xData` is empty then it looks as if you are getting no data returned. – ipr101 Oct 03 '11 at 19:12
  • In that case, I have put it correctly but I still get that nothing is shown. – Larry Morries Oct 04 '11 at 01:10