2

I am attempting to create a glossary tooltip for a website that finds keywords from a json file that is being created by sitecore. I need to get the "Text:" parts from the json file and make then a variable in my jquery so they are the keywords that are found and wrapped with the appropriate tags. I had it working to the point where i could get console to log that there were 2 entries in my json file but that's it.

Here is my sample json code:

[{"Id":"ef339eaa-78e1-4f9e-911e- 096a1920f0b6","Name":"Glossary","DisplayName":"Glossary","TemplateId":"b27d2588-3d02-4f5f-8064-2ee3b7b8eb39","TemplateName":"Glossary","Url":"/Global-Content/Glossary/Glossary","Version":1,"Created":"\/Date(1343987220000)\/","CreatedBy":"sitecore\\rgoodman","Revision":"ae8b3ae0-d0ca-4c4a-9f27-a542a31ab233","Updated":"\/Date(1348137810133)\/","UpdatedBy":"sitecore\\admin","Text":"Glossary","Content":"A bit of test content for the glossary"},{"Id":"3fa51ad4-cfb6-4ff1-a9b5-5276914b2c23","Name":"Abraham","DisplayName":"Abraham","TemplateId":"b27d2588-3d02-4f5f-8064-2ee3b7b8eb39","TemplateName":"Glossary","Url":"/Global-Content/Glossary/A/Abraham","Version":1,"Created":"\/Date(1348148640000)\/","CreatedBy":"sitecore\\admin","Revision":"231284ec-9fb9-4502-ad79-a5806479ecba","Updated":"\/Date(1348148779656)\/","UpdatedBy":"sitecore\\admin","Text":"Abraham","Content":"This is a lincoln person"}]

But I suppose this is not of any use as it is just the "Text:" part i am looking to return.

Here is my jquery:

function getData(url) {
var data;
    $.ajax({
        async: false,
        url: '/_assets/js/glossary.json',
        dataType: 'json',
        success: function(data.Text){
           data.Text = response;
        }
        return(response);
    });
}


function HighlightKeywords(keywords)
{         
var el = $("body");
$(keywords).each(function()
{
    var pattern = new RegExp("(" +this+ ")", ["gi"]);
    var rs = "<mark href='#' class='tooltip'>$1</mark>";
    el.html(el.html().replace(pattern, rs));
});
}        

HighlightKeywords(data.Text);

Essentially i need to return the "Text:" bit of json where data is on the HighlightKerywords function. Where am i going wrong?

Any help would be much appreciated. Thanks

2 Answers 2

3

Your function is not syntactically formatted properly. Your return must go inside of the success function in the synchronous example, and not randomly placed in the ajax object..

function getData() {
    $.ajax({
        async: false,
        url: '/_assets/js/glossary.json',
        dataType: 'json',
        success: function(data){
           //HighlightKeywords(data.Text);
           //or
           return(data.Text);
        }

    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

Ajax is Asynchronous communication, you can't insert its response into a global variable and expect to be able to work with it.

You need to do all the work on the data.text in the success function.

success: function(response){
           HighlightKeywords(response.Text);
        }

1 Comment

He has async set to false. It doesn't really change your answer, but ajax is not always asynch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.