Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Use jquery load() function

Johannes Schwarz
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 10, 2012

Hi everyone,

how can you load the content from another confluence page?

I am trying this, which is not working:

var resFunction = function(data) {
alert(data);
};

var text =AJS.$.get("http://localhost:1990/confluence/plugins/viewsource/viewpagesrc.action?pageId=983079", resFunction);

Produces error:

Error occurred during template rendering: Encountered ")" at /decorators/main.vmd[line 26, column 19] Was expecting one of: "(" ... ... ... ... "##" ... "\\\\" ... "\\" ... ... "*#" ... "*#" ... ... ... ... ... ... ... ... ... ... "{" ... "}" ... . Contact your administrator for assistance.

Any advices?

Thanks!

5 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Matthew J. Horn
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 10, 2012

Start by setting the return value to a string, and then use some regex to extract the contents between the <body> tags. This page shows a simple example:

http://stackoverflow.com/questions/356340/regular-expression-to-extract-html-body-content

Johannes Schwarz
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 10, 2012

thank you :)

Even though the code is not nice it might help anyone some time

&lt;script type="text/javascript"&gt;

AJS.$(document).ready(function(){

var regexPageId = new RegExp("#template:([0-9]*)#");
var oldContent = tinymce.activeEditor.getContent();


if(regexPageId .test(oldContent)) {
var newPageId = regexPageId.exec(oldContent);
var newPageId1 = newPageId[1];

var myContextPath = AJS.contextPath();
var templateUrl = location.protocol+"//"+location.host+""+myContextPath+"/plugins/viewsource/viewpagesrc.action?pageId="+newPageId1;
jQuery.get(templateUrl,  function(data){
var mydata = jQuery.trim(data);
mydata = mydata.replace(/[\n\r\t]/g,''); 

var regex = new RegExp("&lt;body([^&gt;]*)&gt;(.*)&lt;/body&gt;");
var newBody = regex.exec(mydata);
var newBody2 = newBody[2];
jQuery.trim(newBody2);
newBody2.replace(/^(\\s*)/,'');
tinymce.activeEditor.setContent(newBody2);

   });

}

});

&lt;/script&gt;

2 votes
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 11, 2012

Rather than scraping the contents of the view page source action, why don't you just use the REST or JSON-RPC API to get the contents of the desired page in a format that is easy to consume?

AJS.toInit(function() {

  var pageId = 0; // TODO: Populate this value.

  // Build the URL to the REST API.
  var url = AJS.params.contextPath + "/rest/prototype/1/content/" + pageId + ".json";
 
  function useContent(page) {
    AJS.log("Here is the page XHTML - " + page.body.value);
  };
  
  AJS.$.get(url, useContent);
});

EDIT: Here's the full REST API documentation and the SOAP/XML-RPC/JSON-RPC API.

Johannes Schwarz
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 11, 2012

it is working, and way easier. Thanks! However, there are two issues:

1) Line 12: AJS.$.get() is not working for me, I have to use jQuery.get()

2) page.body.value, does not include css classes e.g. for a table. Do you know how to fix this?

Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 11, 2012

How are you loading your javascript into the page? If you're doing it via a plugin, make sure your web resource has a dependency on confluence.web.resources:ajs to make sure that AJS gets loaded first.

Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 11, 2012

Oh, I'm an idiot. I've realised why you were scraping the view source page instead of using the API :)

Scraping the view source page gives you the rendered edit HTML for the page. Calling the REST API gives you the un-rendered storage XHTML for the page. Can you un-accept this answer since the original approach you tried is probably better!

T. Frank June 18, 2012

Hi, I try to get the original approach to work with Confluence 4.1.3, but I get a "tinymce is undefined"
while page with user macro is loaded. Anyone had this experience?

1 vote
Mary Parker May 7, 2017

Hello Johannes,

I think you can use jQuery Load to get content from another page and show then in you current page. 

It will work like this:

$("#myDiv").load("anotherpage.php", function (response, status, xhr) {
    if (status == "error")
        $("#myDiv").html("Error: " + xhr.status + ": " + xhr.statusText);
    });
});

In the above code the jQuery Load will show all HTML content from anotherpage.php in myDiv.

If you see the jQuery Load method uses, then you will find it can be used to grab just a section of the HTML of another page too.

 

See this code now:

$("#myDiv").load("anotherpage.php #someDiv");

In the above line of code, i am just grabbing all the HTML from someDiv only and showing it inside myDiv.

I think  here you can easily use jQuery Load method.

1 vote
Matthew J. Horn
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 10, 2012

I'm guessing that you can't pass just any old raw characters into the alert function. You probably have to escape special characters, like single and double quotes and maybe others. One idea is to wrap the string in the escape() method before calling alert():

alert(escape(data));

hth,

matt

Johannes Schwarz
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 10, 2012

thanks matt. The problem occurs from the javascript code in Line 5. I figured out

jQuery.get("url", resFunction)

returns the content, even though I have no idea how to get body html from that.

1 vote
Dennis Kromhout van der Meer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 10, 2012

Why not just use the Include macro? It will save you a lot of headaches :)

Johannes Schwarz
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 10, 2012

I need the content from another page in the editor and not only an include macro.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events