Results 1 to 7 of 7

Thread: 'Saving' positions in the 'Dom Drag & Drop'

  1. #1
    Join Date
    Sep 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow 'Saving' positions in the 'Dom Drag & Drop' *Unsolved :(

    Hi.
    I was wondering if it was possible to be able to save the positions of the dom drag & drop objects (using cookies, or something like that). The code I am referring to is http://www.dynamicdrive.com/dynamicindex11/domdrag/ .
    (Hopefully) Thanks in advance!
    Last edited by tp4f; 09-03-2007 at 10:49 AM. Reason: I fixed up my spelling

  2. #2
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Well, the basic idea is quite simple, but how involved it gets depends on what you're trying to do. The script supports a onDragEnd event that gives you access to the last known x and y position of the dragged element when the user's mouse button is back up:

    Code:
    <div id="mydiv" style="position: relative; width: 100px; height: 100px; background-color: lightyellow; border: 1px solid black; padding: 3px; z-index: 100">
    Drag me!
    </div>
    
    <script type="text/javascript">
    var mydiv=document.getElementById("mydiv")
    Drag.init(mydiv)
    mydiv.onDragEnd=function(x, y){
    alert(x+" "+y)
    }
    </script>
    In this example, the x,y coordinates is alerted whenever the user finishes dragging. The idea is then to save these numbers into the user's browser cookies, something like:

    Code:
    <script type="text/javascript">
    var mydiv=document.getElementById("mydiv")
    Drag.init(mydiv)
    mydiv.onDragEnd=function(x, y){
    setCookie("mydiv", x+"||"+y, 30)
    }
    
    function getCookie(Name){
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    return ""
    }
    
    function setCookie(name, value, days){
    var expireDate = new Date()
    //set "expstring" to either an explicit date (past or future)
    var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
    document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/"
    }
    </script>
    Finally, the cookie now contains the last known coordinates for the dragged element "mydiv":

    Code:
    alert(getCookie("mydiv"))
    That's the basic idea.

  3. #3
    Join Date
    Sep 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking

    Quote Originally Posted by ddadmin View Post
    Well, the basic idea is quite simple, but how involved it gets depends on what you're trying to do. The script supports a onDragEnd event that gives you access to the last known x and y position of the dragged element when the user's mouse button is back up:

    Code:
    <div id="mydiv" style="position: relative; width: 100px; height: 100px; background-color: lightyellow; border: 1px solid black; padding: 3px; z-index: 100">
    Drag me!
    </div>
    
    <script type="text/javascript">
    var mydiv=document.getElementById("mydiv")
    Drag.init(mydiv)
    mydiv.onDragEnd=function(x, y){
    alert(x+" "+y)
    }
    </script>
    In this example, the x,y coordinates is alerted whenever the user finishes dragging. The idea is then to save these numbers into the user's browser cookies, something like:

    Code:
    <script type="text/javascript">
    var mydiv=document.getElementById("mydiv")
    Drag.init(mydiv)
    mydiv.onDragEnd=function(x, y){
    setCookie("mydiv", x+"||"+y, 30)
    }
    
    function getCookie(Name){
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    return ""
    }
    
    function setCookie(name, value, days){
    var expireDate = new Date()
    //set "expstring" to either an explicit date (past or future)
    var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
    document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/"
    }
    </script>
    Finally, the cookie now contains the last known coordinates for the dragged element "mydiv":

    Code:
    alert(getCookie("mydiv"))
    That's the basic idea.
    Thanks, but just 3 last things. The next time the same user opens that page, will it automaticly open the cookies, so that the divs will go into position?
    Also, where do i put the
    Code:
    alert(getCookie("mydiv"))
    ?
    And lastly, is it possible to make this: http://www.dynamicdrive.com/dynamici...edcollapse.htm draggable?

    Thank you so much, whoever answers these
    Last edited by tp4f; 09-03-2007 at 10:33 AM.

  4. #4
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    No, the alert() line I provided is just for illustration, so you can see that the coordinates are being stored. Here's a full working example of a DIV with its dragged coordinates remembered, so reloading the page causes the DIV to move back to its last known position:

    Code:
    <div id="mydiv" style="position: relative; width: 100px; height: 100px; background-color: lightyellow; border: 1px solid black; padding: 3px; z-index: 100">
    Drag me!
    </div>
    
    <script type="text/javascript">
    var mydiv=document.getElementById("mydiv")
    Drag.init(mydiv)
    mydiv.onDragEnd=function(x, y){
    setCookie("mydiv", x+"||"+y, 30)
    }
    
    if (getCookie("mydiv")!=""){
    var mydivcookie=getCookie("mydiv").split("||")
    document.getElementById("mydiv").style.left=parseInt(mydivcookie[0])+"px"
    document.getElementById("mydiv").style.top=parseInt(mydivcookie[1])+"px"
    }
    
    function getCookie(Name){
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    return ""
    }
    
    function setCookie(name, value, days){
    var expireDate = new Date()
    //set "expstring" to either an explicit date (past or future)
    var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
    document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/"
    }
    
    </script>
    And lastly, is it possible to make this: http://www.dynamicdrive.com/dynamici...edcollapse.htm draggable?
    Now we digress.

  5. #5
    Join Date
    Sep 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by ddadmin View Post
    No, the alert() line I provided is just for illustration, so you can see that the coordinates are being stored. Here's a full working example of a DIV with its dragged coordinates remembered, so reloading the page causes the DIV to move back to its last known position:

    Code:
    <div id="mydiv" style="position: relative; width: 100px; height: 100px; background-color: lightyellow; border: 1px solid black; padding: 3px; z-index: 100">
    Drag me!
    </div>
    
    <script type="text/javascript">
    var mydiv=document.getElementById("mydiv")
    Drag.init(mydiv)
    mydiv.onDragEnd=function(x, y){
    setCookie("mydiv", x+"||"+y, 30)
    }
    
    if (getCookie("mydiv")!=""){
    var mydivcookie=getCookie("mydiv").split("||")
    document.getElementById("mydiv").style.left=parseInt(mydivcookie[0])+"px"
    document.getElementById("mydiv").style.top=parseInt(mydivcookie[1])+"px"
    }
    
    function getCookie(Name){
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    return ""
    }
    
    function setCookie(name, value, days){
    var expireDate = new Date()
    //set "expstring" to either an explicit date (past or future)
    var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
    document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/"
    }
    
    </script>


    Now we digress.
    It still isn't working. I copied that code word for word, added this to the end of the page:
    Code:
    Drag.init(document.getElementById("mydiv"));
    It just (when you reload the page) goes back to its original position.

  6. #6
    Join Date
    Sep 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I've...

    I've uploaded the actual page (in a zip). The actual drag works, but when i reaload the page, it goes back to its original spot. Can you please only put the script on "mydiv"?
    Thank you!!!

    PS: I think the problem is I've put the code in the wrong spot

  7. #7
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    So it works for you now? I just tested what I had posted, and it does work (reloading page still remembers last dragged positions).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •