Solved

Javascript Question: Url click insert data in the database

Posted on 2009-03-31
7
209 Views
Last Modified: 2012-05-06
Hi ,

I have pasted the code below:
Currently, it will display
First Link        Dismiss
Page 1 of 4 Next

Second Link  Dismiss
Page 2 of 4 Next

I want that when Dismiss is clicked for first link, i will save Url, sTitle, sLabel for first link in the database.
If for second link, dismiss is clicked, that will be save in the database.
I can call asp.net webservice through jAVASCRIPT so that I can pass pass these parameters and that save in database.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><!--$include ss_layout_head_info-->
<script type="text/javascript" language="javascript">
var curlink = 0;
var oXMLDOM = new ActiveXObject("Microsoft.XMLDOM");
var myReq = (window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP");
var sDisplayDiv = "area";
 
function onXMLResponse()
    {
        var sResponse = "";
                if(myReq.readyState == 4)
                    {
                        if(myReq.getResponseHeader('Content-Type').indexOf('xml') != -1)
                            {
                                sResponse = parseXML(myReq.responseXML);	
                            } 
  document.getElementById(sDisplayDiv).innerHTML = sResponse; ////Main Response
                        initLinks();
                    }
        }//onXMLResponse
        
    function parseXML(sXML)
        {
	        var sReturn = "";
	        var oLinkNode = false;
 
	        if(oXMLDOM != null && sXML != "")
	            {
		            oXMLDOM.async=false;
		            oXMLDOM.loadXML(sXML);
	            }
	        oRootNode = oXMLDOM.documentElement.firstChild;
	         for(var i=0; i<ResultSet.length; i++)
   	                    {   
                             oLinkNode = ResultSet[i];
                             Url = oLinkNode.getAttribute("dUrl");			// get URL
                             sTitle = oLinkNode.getAttribute("title");	// GET URLTITLE
                             sLabel = oLinkNode.getAttribute("Label");
                             sReturn += "<a href=" + sUrlTitle + ">" + sUrl + "</a>";
                             
                     }//for
                        
	                return sReturn;
	            }
        }//parseXML
  
     
    function getLinks () {
	var mydiv = document.getElementById('area');
	var links = mydiv.getElementsByTagName('A');
	return (links);
}
 
function setLink (whichLink) {
 
	var links = getLinks();
	for (i=0;i<links.length;i++) {
		links[i].style.display = (i == whichLink) ? '' : 'none';
	}
	var ltext ='(' + " " + (whichLink+1) + ' of ' + links.length + ' Links)';
	if (whichLink != 0) {
		var prevtext = '<a href="#" onclick="setLink('+ (whichLink-1) + ')" style="color:#688FCF;font-size:1em;">Previous Alert</a> ';
		ltext = prevtext + ltext;
	}
	if (whichLink != (links.length-1)) {
		var nexttext = ' <a href="#" onclick="setLink(' + (whichLink+1) + ')" style="color:#688FCF;font-size:1em;">Read next alert >></a>';
		ltext = ltext + nexttext;
	}
 
	document.getElementById('linktext').innerHTML = ltext;
}
    
      window.onload=function()
        {
            makeRequest();
        }
 
    function makeRequest()
        {
	        var sUrl = "http://ngfff.com/"
   		        myReq.onreadystatechange = onXMLResponse;
		            myReq.open("POST", sUrl, true); // true indicates asynchronous request
		            myReq.setRequestHeader("Content-Type", "text/xml");
		            myReq.send(strSoapRequest);
            }
 
</script>
 
</head>
 
<body>
 
 
       <span id="area" style="padding:14px;margin-right:20px;"></span>
       <span id="dismiss"><a href="#" onclick="dismissLink();" style="margin-left:20px;">Dismiss>></a></span>
        <div id="linktext"></div>
 
 
</body>
</html>

Open in new window

0
Comment
Question by:Saroj13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

Author Comment

by:Saroj13
ID: 24036011
Hello,

Please provide me the solution of how to get the data of url when dismiss click on every page.

First Link   Dismiss
Page 1 of 4 Next

When Dismiss is clicked, I need the information of current url like urltitle, url and label.

Thanks
0
 
LVL 14

Expert Comment

by:ziffgone
ID: 24040335
What is sTitle and sLabel? And what do you mean by urltitle ?

If you mean save the url of the page, the title of the page, and I have no idea what you mean by label, here's what I come up with...sorta of:




function dismissLink(){
   var url=top.location.href.toString();
   var ttl=document.title;
   var label=????;
 
   // do stuff with info
 
 
   alert('URL: '+url);
   alert('Title: '+ttl);
 
}

Open in new window

0
 

Author Comment

by:Saroj13
ID: 24040793
Hi,
I mean by this:
 function parseXML(sXML)
        {
              var sReturn = "";
              var oLinkNode = false;
 
              if(oXMLDOM != null && sXML != "")
                  {
                        oXMLDOM.async=false;
                        oXMLDOM.loadXML(sXML);
                  }
              oRootNode = oXMLDOM.documentElement.firstChild;
               for(var i=0; i<ResultSet.length; i++)
                             {  
                             oLinkNode = ResultSet[i];
                             sUrl = oLinkNode.getAttribute("dUrl");                  // get URL
                             sTitle = oLinkNode.getAttribute("title");      // GET URLTITLE
                             sLabel = oLinkNode.getAttribute("Label");
                             sReturn += "<a href=" + sUrlTitle + ">" + sUrl + "</a>";                          
                     }//for
      return sReturn;          }
        }//parseXML

Then I am assigning this sReturn in <div>

Problem:...I am displaying data in this way:  
 First Link              dismiss
Page 1 of 4 Next

How dismiss link will get the information like sLabel, sUrl, SUrlTitle??

Thanks
 
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 14

Expert Comment

by:ziffgone
ID: 24041346
Ok, first off if it doesn't already have an ID, give the <div> that holds the sReturn value an id, like this:

<div id="_sReturn_">

Or something like that. Then use the below function.

Hope this helps.

function dismissLink(){
   var gUrl=document.getElementById('_sReturn_').getElementsByTagName('a')[0];
 
   var saveUrl=gUrl.getAttribute("href");
 
   alert(saveUrl);
 
   // maybe store in cookie:
 
   document.cookie='savedUrl='+saveUrl+';';
 
}

Open in new window

0
 
LVL 14

Accepted Solution

by:
ziffgone earned 500 total points
ID: 24041441
Or maybe this is what you're after to utilize all info. Note I had to edit your parseXML(sXML) function so all values were included in the sReturn link passed to you div.

sUrl
sTitle
sLabel

Hope this is it. :)
 function parseXML(sXML)
        {
              var sReturn = "";
              var oLinkNode = false;
 
              if(oXMLDOM != null && sXML != "")
                  {
                        oXMLDOM.async=false;
                        oXMLDOM.loadXML(sXML);
                  }
              oRootNode = oXMLDOM.documentElement.firstChild;
               for(var i=0; i<ResultSet.length; i++)
                             {  
                             oLinkNode = ResultSet[i];
                             sUrl = oLinkNode.getAttribute("dUrl");                  // get URL
                             sTitle = oLinkNode.getAttribute("title");      // GET URLTITLE
                             sLabel = oLinkNode.getAttribute("Label");
                             sReturn += "<a href=" + sUrl + " title="+ sLabel + ">" + sTitle + "</a>";                          
                     }//for
      return sReturn;          }
        }//parseXML
 
 
// ################################333
 
 
function dismissLink(){
   var gUrl=document.getElementById('_sReturn_').getElementsByTagName('a')[0];
 
   var get_sUrl=gUrl.getAttribute("href");
   var get_sLabel=gUrl.getAttribute("title"); 
   var get_sTitle=gUrl.innerHTML;
   alert('sUrl: ' + get_sUrl + '\nsTitle: '+ get_sTitle + '\nsLabel: ' + get_sLabel);
 
   // maybe store in cookie:
 
   document.cookie='sUrl='+get_sUrl+';sTitle='+get_sTitle+';sLabel='+get_sLabel+';';
 
}

Open in new window

0
 

Author Comment

by:Saroj13
ID: 24041931
There is a change in the code. I am calling initLinks() from ParseXml.

Dismiss link, I am doing two things... I am getting the data for same link and that same link I am dismissing.

Suppose, I am on
Second Link          Dismiss
Previous Page 2 of 4 links Next

What dismiss is doing is it will dismiss the second link and I want the value of dismissed link

Now it will have First Link Dismiss
Page 1 of 3 Next

Would you please make changes in the DismissLink function so that it will work the same. Dismissing the link is working fine, just I want is I want to retrive the value of the dismmied link.

Thanks


    function initLinks () 
        {
             setLink(0);
	        init();
        }//initLinks
 
    function getLinks () 
        {
	        var mydiv = document.getElementById(area');
	        var links = mydiv.getElementsByTagName('A');
	        return (links);
        }//getLinks
 
    function setLink (whichLink) 
        { 
            curlink = whichLink;
	        var links = getLinks();
	        for (i=0;i<links.length;i++) 
	            {
             
                    if(i == whichLink)
                        {
                            links[i].style.display = '';
                            document.getElementById('readmore').childNodes[0].href=links[i].href;
                        } else 
                        {
                            links[i].style.display = 'none';
                        } 
 
}	
              document.getElementById('dismiss').style.visibility="visible";
 
	        var ltext ='&nbsp;&nbsp;(' + " " + (whichLink+1) + ' of ' + links.length + ' page)';
	
	        if (whichLink != 0) 
	            {
		            var prevtext = '&nbsp;&nbsp;<a href="#" onclick="setLink('+ (whichLink-1) + ')" style="color:#688FCF;font-size:1em;">Previous </a> ';
		            ltext = prevtext + ltext;
	            }
	        if (whichLink != (links.length-1)) 
	            {
		            var nexttext = ' <a href="#" onclick="setLink(' + (whichLink+1) + ')" style="color:#688FCF;font-size:1em;">Read next >></a>';
		            ltext = ltext + nexttext;
	            }
	       
	        
	        document.getElementById('linktext').innerHTML = ltext; 
	        if(links.length == 0)
	            {
                    document.getElementById('linktext').style.display = "none";
                    document.getElementById('dismiss').style.display = "none";
                    document.getElementById('Msg').style.display = "none";
                }
	 return false;       
    }//setLink
    
 
    function dismissLink (whichLink) 
        {   
            var gUrl= document.getElementById(area').getElementsByTagName('a')[0];
 
            var get_sUrl=gUrl.getAttribute("href");
            var get_sLabel=gUrl.getAttribute("title"); 
            var get_sTitle=gUrl.innerHTML;
            
            alert(get_sUrl);
            
            var links = getLinks();
            var mydiv = document.getElementById('area');
            saveIt('links'+links[whichLink].href);
            mydiv.removeChild(links[whichLink]);
            setLink(0);
            
            
            
 
 
        }
       
    function createCookie(name,value,days) 
        {
	        if (days) 
	            {
		            var date = new Date();
		            date.setTime(date.getTime()+(days*24*60*60*1000));
		            var expires = "; expires="+date.toGMTString();
	            }
	        else var expires = "";
	            document.cookie = name+"="+value+expires+"; path=/";
        }
        
    function readCookie(name) 
        {
	        var nameEQ = name + "=";
	        var ca = document.cookie.split(';');
	        for(var i=0;i < ca.length;i++) 
	            {
		            var c = ca[i];
		            while (c.charAt(0)==' ') c = c.substring(1,c.length);
		            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	            }
	        return null;
        } 
        
    function saveIt(name) 
        {
            var x = 1;
	        createCookie(name,x,7);
        }
     
     function init() 
        {
            var links = getLinks();
	        for (var i=3;i>=0;i--) 
	            {
		            var x = readCookie('links' + links[i].href);
                    if(x)
                        {
                            dismissLink(i);
                        }
	            }
        }

Open in new window

0
 

Author Comment

by:Saroj13
ID: 24042178
Thanks alot. Issue is resolved.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question