[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1511
  • Last Modified:

Parse links for use in other code

Hello,

I would like the code below to split up the href tag into a URL and into link text for two different types of links (if it's possible).

The first link would be this type: <a href="http://www.yahoo.com">Yahoo</a>
The results should be: http://www.yahoo.com   &  Yahoo  (this works in the code below)

The second type of link is: <a href="javascript:link('speciallink')">Special Link</a>
The results should be: javascript:link('speciallink')  &  Special Link

Thanks in advance for any help.

 <script language="JavaScript">
var s = '<a href="http://www.yahoo.com">Yahoo Site </a>'
var regx = /^.*\=\"(.*)\">(.*)<.*$/;
regx = regx.exec(s);
var url = ''
var text = ''
if (regx && regx[1] ) {
url = regx[1]
text = regx[2]
// create your new Option(url, text)

// add it to the select
}
document.write(text)
document.write(url)

</script>
0
peazak
Asked:
peazak
  • 9
  • 8
1 Solution
 
hieloCommented:
The code provided is already splitting both link formats. I tried the following:
Input: s = '<a href="http://www.yahoo.com">Yahoo Site</a>';
Output:Yahoo Sitehttp://www.yahoo.com

Input: s='<a href="javascript:link(\'speciallink\')">Special Link</a>'
Output: Special Linkjavascript:link('speciallink')
0
 
peazakAuthor Commented:
Hielo - I can't add backslashes to the second link.

It has to remain as: <a href="javascript:link('speciallink')">Special Link</a>
0
 
hieloCommented:
Where are you reading/getting input from: a form element? an input window? a database?
The added backslashes are just so that the string is assigned to a variable. If for instance the input is on a text form element, you could say:
<input type="text" name="inputLink" id="inputLink" value=""/>
once the user inputs the link above(no backslashes necessary), then you could assign it to "s" as shown below and the rest of the code works OK.
s = document.getElementById('inputLink').value;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
peazakAuthor Commented:
The web page has been built in a content management system called Stellent. The links are saved in an XML file. They are normally displayed using Stellent's coding language called iDoc Script (not an xsl sheet).

Here's an example of the XML:
- <Item>
  <a href="http://www.link1.com">Link 1</a>
  </Item>
- <Item>
  <a href="javascript:link('Link2');">Link2</a>
  </Item>
- <Item>

The iDoc Script loops through and displays each "Item" as I gave them to you.  I was trying to use JavaScript after the loop but before the display so that I could create different types of menus within the content management system (right now it appears as a list but I wanted drop-downs). I really don't know of a way to manipulate the links until after idoc script has retrieved them.
0
 
hieloCommented:
This now makes sense, since once you "extract" the href from the elements, it would be stored in some variable already. In the example that you gave you are literally hardcoding the values and you must escape the apostrophes some how( backslashes, &#39; hex value).

Based on your comments it seems you are ultimately only seeing html links: ie <a href="">...</a>
Try this:
var linkList=document.getElementsByTagName('a');
for(var i=0;i<linkList.length;i++)
 document.write(linkList.href + " " + linkList.value);

0
 
peazakAuthor Commented:
Hielo - I appreciate your help but I couldn't get that to work. Below is the drop-down that I'm trying to create using HTML, iDoc Script and JavaScript. The JavaScript code that I provided above is the same except this uses idoc variables.

<select name="initiatives" class="nav_form_list" onChange="MM_jumpMenu('parent',this,0)">
                           <!--$ loopwhile nPos <= nNumRecords -->
                              <!-- Row -->
                              <script language="JavaScript">
                                    var s<!--$nPos--> = '<!--$ ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()") -->'
                                    var regx<!--$nPos--> = /^.*\=\"(.*)\">(.*)<.*$/;
                                    regx<!--$nPos--> = regx<!--$nPos-->.exec(s<!--$nPos-->);
                                    var url<!--$nPos--> = ''
                                    var text<!--$nPos--> = ''
                                    if (regx<!--$nPos--> && regx<!--$nPos-->[1] ) {
                                    url<!--$nPos--> = regx<!--$nPos-->[1]
                                    text<!--$nPos--> = regx<!--$nPos-->[2]
                                    }

                              </script>       
                                     <script>document.write("<option value='"+url<!--$nPos-->+"'>"+text<!--$nPos-->+"</option>")</script>      
                              <!--$nPos=nPos+1-->
                        <!--$endloop-->
                        </select>

AND Here is the same code as it appears on the page. The yahoo and ebay links are the only ones that actually appear.

                      <select name="initiatives" class="nav_form_list" onChange="MM_jumpMenu('parent',this,0)">
                           
                              <!-- Row -->
                              <script language="JavaScript">
                                    var s1 = '<a href="http://www.ebay.com">Link 3</a>'
                                    var regx1 = /^.*\=\"(.*)\">(.*)<.*$/;
                                    regx1 = regx1.exec(s1);
                                    var url1 = ''
                                    var text1 = ''
                                    if (regx1 && regx1[1] ) {
                                    url1 = regx1[1]
                                    text1 = regx1[2]
                                    }

                              </script>       
                                     <script>document.write("<option value='"+url1+"'>"+text1+"</option>")</script>      
                              
                        
                              <!-- Row -->
                              <script language="JavaScript">
                                    var s2 = '<a href="http://www.yahoo.com">Link 2</a>'
                                    var regx2 = /^.*\=\"(.*)\">(.*)<.*$/;
                                    regx2 = regx2.exec(s2);
                                    var url2 = ''
                                    var text2 = ''
                                    if (regx2 && regx2[1] ) {
                                    url2 = regx2[1]
                                    text2 = regx2[2]
                                    }

                              </script>       
                                     <script>document.write("<option value='"+url2+"'>"+text2+"</option>")</script>      
                              
                        
                              <!-- Row -->
                              <script language="JavaScript">
                                    var s3 = '<a href="javascript:nodelink('2162');">Link 1</a>'
                                    var regx3 = /^.*\=\"(.*)\">(.*)<.*$/;
                                    regx3 = regx3.exec(s3);
                                    var url3 = ''
                                    var text3 = ''
                                    if (regx3 && regx3[1] ) {
                                    url3 = regx3[1]
                                    text3 = regx3[2]
                                    }

                              </script>       
                                     <script>document.write("<option value='"+url3+"'>"+text3+"</option>")</script>      
                              
                        
                        </select>
                        <script>
0
 
hieloCommented:
OK. I See. Try using a different approach altogether.
What I would do is to Write all the links into the HTML document first and then retrieve them via javascript once the document loads

<div id="linkList" style="position:absolute;top:-10em;left:-10em;">
      <!--$ ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()") -->
</div>

<!-- this is meant as a temporary place holder of the select list, which will be inserted via javascript once the document loads -->
<div id="selectListHolder"></div>

Now that all the links are grouped together under the "linkList" div,
<script type="text/javascript">
function populateSelect()
{
      var linkGroup = document.getElementById("linkList");
      var links=linkGroup.getElementsByTagName("a");
      var sel="";
      if(links)
      {
            sel+="<select>";
            for(var i=0; i< links.length;++i)
            {
                  sel+="<option value='" + links[i].href + "'>" + links[i].text + "</option>";
            }
            sel+="</select>";
            document.getElementById("selectListHolder").innerHTML=sel;
      }
      linkGroup=null
      links=null
return true;
}

window.onload=populateSelect;
</script>
0
 
hieloCommented:
Update: since the href values may contain apostrophes, you are better off writing the statement within the for loop as follows:
sel+='<option value="' + links[i].href + '">' + links[i].text + '</option>';
Instead of enclosing the value attribute in apostrophes I am now enclosing it in double quotes.
0
 
hieloCommented:
OK. Try this:
var s<!--$nPos--> = '<!--$ strReplace(ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()"),"'","\'" -->'
 instead of:
var s<!--$nPos--> = '<!--$ ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()") -->'
If that does not help then try:
var s<!--$nPos--> = '<!--$ strReplace(ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()"),"'","&#39;" -->'
0
 
peazakAuthor Commented:
Heilo - i really appreciate your help. Something urgent just came up but I should be able to get back to this issue this afternoon. I'll let you know the results ASAP.
0
 
peazakAuthor Commented:
hielo - I used your updated var s declaration (I tried both).  Here's what I tried and it doesn't work:

<select name="initiatives" class="nav_form_list" onChange="MM_jumpMenu('parent',this,0)">
                           <!--$ loopwhile nPos <= nNumRecords -->
                              <!-- Row -->
                              <script language="JavaScript">
var s<!--$nPos--> = '<!--$ strReplace(ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()"),"'","&#39;" -->'
                                    var regx<!--$nPos--> = /^.*\=\"(.*)\">(.*)<.*$/;
                                    regx<!--$nPos--> = regx<!--$nPos-->.exec(s<!--$nPos-->);
                                    var url<!--$nPos--> = ''
                                    var text<!--$nPos--> = ''
                                    if (regx<!--$nPos--> && regx<!--$nPos-->[1] ) {
                                    url<!--$nPos--> = regx<!--$nPos-->[1]
                                    text<!--$nPos--> = regx<!--$nPos-->[2]
                                    }

                              </script>       
                                     <script>document.write("<option value='"+url<!--$nPos-->+"'>"+text<!--$nPos-->+"</option>")</script>      
                              <!--$nPos=nPos+1-->
                        <!--$endloop-->
                        </select>
                        <script>      

I also tried my own version of it:

var s<!--$nPos--> = '<!--$ strReplace(ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()" & "'" & "\'")) -->'

and

var s<!--$nPos--> = '<!--$ strReplace(ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()" & "'" & "&#39'")) -->'

I think this may not be possible.
0
 
hieloCommented:
I know what needs to be done, but I have never seen/heard of iDoc before so I don't know its syntax.
I went to the following site for Technical Reference:
http://tariffs.qwest.com:8000/idc/help/sdk/idoc_script_reference/wwhelp/wwhimpl/js/html/wwhelp.htm?href=page_6_001.htm
However, I am questioning if that is the correct online technical reference because I was not able to find any documentation on the ssIncludeXml. This is where I got the strReplace last time. Hence my last post.

Basically what we need to do is as follows:
a Instead of "printing" the line that is giving us problems we need to save string returned by the ssIncludeXml function onto a variable.
b escape the apostrophes
c print the escaped string

My problem is
1. if ssIncludeXms automatically prints or just returns a string
2. I don't know how to save data onto variables,
3. I don't know how to escape charactes.

What follows is what I came up with after some careful analysis of your first "full" code post. If I did things correctly, then the output would be:
var s3 = '<a href="javascript:nodelink(x2162x);">Link 1</a>'

I know it is not valid javascript, but if you see that line, then it is just a matter of figuring out how to escape the apostrophe in iDoc Script. In other words, if the code below give the result mentioned, then it is a matter of changing the "x" (without the quotes) to hopefully one of the following:
&#39;
\x27
\\'
\\\'

At any rate, try this code snippet.
<select name="initiatives" class="nav_form_list" onChange="MM_jumpMenu('parent',this,0)">
 <!--$ loopwhile nPos <= nNumRecords -->
  <!-- Row -->
  <script type="text/javaScript">
  <!--$tempData=ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()") -->
  <!--$tempData=strReplace($tempData,"'","x") -->
  var s<!--$nPos--> = '<!--$tempData-->'
  var regx<!--$nPos--> = /^.*\=\"(.*)\">(.*)<.*$/;
  regx<!--$nPos--> = regx<!--$nPos-->.exec(s<!--$nPos-->);
  var url<!--$nPos--> = ''
  var text<!--$nPos--> = ''
  if (regx<!--$nPos--> && regx<!--$nPos-->[1] ) {
   url<!--$nPos--> = regx<!--$nPos-->[1]
   text<!--$nPos--> = regx<!--$nPos-->[2]
  }
  </script>      
  <script type="text/javascript">document.write("<option value='"+url<!--$nPos-->+"'>"+text<!--$nPos-->+"</option>")</script>      
  <!--$nPos=nPos+1-->
 <!--$endloop-->
</select>
0
 
peazakAuthor Commented:
Hielo - That was a really great idea. It doesn't seem to work because the idoc script doesn't seem to "print" the link out, until after it's on a webpage. I modified your code and still didn't have much luck (see below). I posted this question to a Stellent user group as well. Maybe someone else has some ideas. If you can't think of anything else, please let me know because I'll gladly give you the solution points for your effort.

                      <select name="initiatives" class="nav_form_list" onChange="MM_jumpMenu('parent',this,0)">
 <!--$ loopwhile nPos <= nNumRecords -->
  <!-- Row -->
  <script type="text/javaScript">
  <!--$tempData = ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()") -->
  <!--$printLink = tempData-->
  <!--$newLink = regexReplaceAll($tempData, "'", "x")-->
  var s<!--$nPos--> = '<!--$newLink-->'
    var g<!--$nPos--> = '<!--$tempData-->'
  var regx<!--$nPos--> = /^.*\=\"(.*)\">(.*)<.*$/;
  regx<!--$nPos--> = regx<!--$nPos-->.exec(s<!--$nPos-->);
  var url<!--$nPos--> = ''
  var text<!--$nPos--> = ''
  if (regx<!--$nPos--> && regx<!--$nPos-->[1] ) {
   url<!--$nPos--> = regx<!--$nPos-->[1]
   text<!--$nPos--> = regx<!--$nPos-->[2]
  }
  </script>
  <!--$tempData-->  
    <!--$printLink-->    
      <!--$newLink-->
  <script type="text/javascript">document.write("<option value='"+url<!--$nPos-->+"'>"+text<!--$nPos-->+"</option>")</script>      
  <!--$nPos=nPos+1-->
 <!--$endloop-->
</select>
0
 
hieloCommented:
For now, just for now,  comment out
<!--$newLink = regexReplaceAll($tempData, "'", "x")-->

Then, on the code you posted, change:
<!--$tempData = ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()") -->
 TO
<!--$exec tempData = ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()") -->
<!--$tempData-->
if that still does not show any output, then try:
<!--$exec tempData = ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()") -->
<!--$eval tempData-->
instead.
0
 
peazakAuthor Commented:
This code displays the link. But that's about it.

<!--$exec tempData = ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()") -->
<!--$tempData-->

The other code doesn't work.

0
 
hieloCommented:
Good, that is exactly what we wanted. To store the link on tempData. Now we need to focus on how to do the apostrophe replacement. We may need to try various escape sequences.  Use the following code. We have been using:
<!--$newLink = regexReplaceAll($tempData, "'", "x")-->

and upon closer inspection I believe the correct usage is:
<!--$newLink = regexReplaceAll(tempData, "'", "x")-->

The difference is the lack of the dollar sign in the first argument to replaceAll.
At any rate, the code variations that you will need to attempt are:
<!--$exec newLink = regexReplaceAll(tempData, "'", "x")-->
<!--$exec newLink = regexReplaceAll(tempData, "\'", "x")-->
<!--$exec newLink = regexReplaceAll(tempData, "\x27", "x")-->

Notice that other changes on the code below are the exec statements. So make sure you update your code from what I am providing. Also, if any of the variations above correctly repaces the apostrophe with an x, then you go back and instead of "x" use "&#39;"

Here is the updated code.
<select name="initiatives" class="nav_form_list" onChange="MM_jumpMenu('parent',this,0)">
 <!--$ loopwhile nPos <= nNumRecords -->
  <!-- Row -->
  <script type="text/javaScript">
  <!--$exec tempData = ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()") -->
  <!--$exec printLink = tempData-->
  <!--$exec newLink = regexReplaceAll( tempData, "'", "x")-->
  var s<!--$nPos--> = '<!--$newLink-->'
    var g<!--$nPos--> = '<!--$tempData-->'
  var regx<!--$nPos--> = /^.*\=\"(.*)\">(.*)<.*$/;
  regx<!--$nPos--> = regx<!--$nPos-->.exec(s<!--$nPos-->);
  var url<!--$nPos--> = ''
  var text<!--$nPos--> = ''
  if (regx<!--$nPos--> && regx<!--$nPos-->[1] ) {
   url<!--$nPos--> = regx<!--$nPos-->[1]
   text<!--$nPos--> = regx<!--$nPos-->[2]
  }
  </script>
  <!--$tempData-->  
    <!--$printLink-->    
      <!--$newLink-->
  <script type="text/javascript">document.write("<option value='"+url<!--$nPos-->+"'>"+text<!--$nPos-->+"</option>")</script>      
  <!--$nPos=nPos+1-->
 <!--$endloop-->
</select>back to top
0
 
peazakAuthor Commented:
Hielo - I found some idoc script that adds backslashes to escape white space and quotes. It works now. Now I just need to get the drop-down to open nodelink pages. Thanks for all of your help.

Code:

 <select name="initiatives" class="nav_form_list" onChange="MM_jumpMenu('parent',this,0)">
 <!--$ loopwhile nPos <= nNumRecords -->
  <!-- Row -->
  <script type="text/javaScript">
<!--$tempData = ssIncludeXml(SS_DATAFILE, root & "/" & "Item" & "[" & nPos & "]/node()") -->
  var s<!--$nPos--> = '<!--$js(tempData)-->'
  var regx<!--$nPos--> = /^.*\=\"(.*)\">(.*)<.*$/;
  regx<!--$nPos--> = regx<!--$nPos-->.exec(s<!--$nPos-->);
  var url<!--$nPos--> = ''
  var text<!--$nPos--> = ''
  if (regx<!--$nPos--> && regx<!--$nPos-->[1] ) {
   url<!--$nPos--> = regx<!--$nPos-->[1]
   text<!--$nPos--> = regx<!--$nPos-->[2]
  }
  </script>
  <!--$tempData-->  
      <!--$js(tempData)-->
  <script type="text/javascript">document.write("<option value='"+url<!--$nPos-->+"'>"+text<!--$nPos-->+"</option>")</script>      
  <!--$nPos=nPos+1-->
 <!--$endloop-->
</select>

Results:

  <!-- Row -->
  <script type="text/javaScript">

  var s2 = '<a href=\"http://www.yahoo.com\">Link 2</a>'
  var regx2 = /^.*\=\"(.*)\">(.*)<.*$/;
  regx2 = regx2.exec(s2);
  var url2 = ''
  var text2 = ''
  if (regx2 && regx2[1] ) {
   url2 = regx2[1]
   text2 = regx2[2]
  }
  </script>
  <a href="http://www.yahoo.com">Link 2</a>  
      <a href=\"http://www.yahoo.com\">Link 2</a>
  <script type="text/javascript">document.write("<option value='"+url2+"'>"+text2+"</option>")</script>      
 
 
  <!-- Row -->
  <script type="text/javaScript">

  var s3 = '<a href=\"javascript:nodelink(\'2162\');\">Link 1</a>'
  var regx3 = /^.*\=\"(.*)\">(.*)<.*$/;
  regx3 = regx3.exec(s3);
  var url3 = ''
  var text3 = ''
  if (regx3 && regx3[1] ) {
   url3 = regx3[1]
   text3 = regx3[2]
  }
  </script>
  <a href="javascript:nodelink('2162');">Link 1</a>  
      <a href=\"javascript:nodelink(\'2162\');\">Link 1</a>
  <script type="text/javascript">document.write("<option value='"+url3+"'>"+text3+"</option>")</script>      
 
 
</select>

Can you help me get the javascript to use the nodelink correctly?
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now